Page 1 of 1
Simple Database Question
Posted: Thu Dec 20, 2007 4:17 pm
by justinsl
I have a database and I would like to know how to pull certain fields via PHP.
For example: Mysql Database name = products
id
name
type
misc1
... and so on
I want the ability to call this information from the database instead of having to manually type this information. Say I have a page for each product. Im trying to figure out how to create a template that every product will use and somewhere identify it my id or name then have the various fields pulled from the database and planted in my page.
My Product page
Product ID: (Pulled id from database)
Name: (Pulled name from database)
Type: (Pulled type from database)
Misc1: (Pulled misc1 from database)
I do not need any type of query or form to search I just want the ability to edit my database (via filemaker 9 or imported csv) which I can do now. and then have the updated information cascade on to all my pages.
Im looking for help/links to tutorials I just dont want to waste too much time learning about the really heavy stuff when It doesnt sound too hard to pull fields from a database and display it where I want it. I also want this cross platform. I had seen different ways to parse xml files for info and display then on a page and I know that there are certain browser specific.
-Justin
Re: Simple Database Question
Posted: Thu Dec 20, 2007 8:58 pm
by califdon
justinsl wrote:Im looking for help/links to tutorials I just dont want to waste too much time learning about the really heavy stuff when It doesnt sound too hard to pull fields from a database and display it where I want it. I also want this cross platform. I had seen different ways to parse xml files for info and display then on a page and I know that there are certain browser specific.
I'm afraid that anything that you need to do with a database requires a minimum skill level with SQL and PHP and HTML. It may not sound "too hard" to you, but you have to use all the same syntax and program logic to do the simplest things, that you would need to do rather complicated things. I recommend that you read any good beginning tutorial. Depending on what skills you have to begin with, go through the appropriate tutorials at
http://w3schools.com, for example. That's a good place to start. There is nothing special to do to be cross-browser, until you get into rather advanced CSS and javascript.
Database Question
Posted: Fri Dec 21, 2007 8:37 am
by justinsl
I didnt mean to sound as if I was not wanting to spend the time to learn. Let me ask you this.
Tell me if I am on the right track.
Would I go about it in this order?
1. Connect to the Database
2. Ask the database to find the row with "productnamegoeshere"
2. Display the fields in the row on the page
4 Close the database
So if I call that row I can then echo them into the page?
I am assuming that I can learn how to (providing PHP can which im sure it can) have the code take the name of the page itself and use it as a var to query the database. Like "product2.php" the page will pull its own name in as a var and use it to query the database where I might have a field called "pagename"
Ill have to look it up but my site layout is 100% CSS, crossbrowser and validates im wondering if php is going to toss a wrench into it?
Thank You,
Justin
Posted: Fri Dec 21, 2007 2:11 pm
by Chalks
justinsl wrote:1. Connect to the Database
2. Ask the database to find the row with "productnamegoeshere"
3. Display the fields in the row on the page
4 Close the database
Pretty much. Here are some functions that will probably help you for each step:
1:
mysql_connect(),
mysql_select_db()
2:
mysql_real_escape_string(),
mysql_query(). Use a search string similar to:
Code: Select all
SELECT rowName, anotherRowName FROM tableName WHERE id=102
3:
mysql_fetch_array() This step is a little confusing, example below.
4:
mysql_close()
EXAMPLE:
Code: Select all
<?php
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error());
mysql_select_db($db);
// Step 1 complete... connected to database.
$pull_this_row = "userName"; // you can query the database many different ways the w3schools tutorial mentioned above is a good starter. Also, if you are allowing user input to go into this variable, be sure to use mysql_real_escape_string()
$sql = "SELECT * FROM tableName WHERE user='$pull_this_row'";
$result = mysql_query($sql) or die(mysql_error());
// Step 2 complete.
?>
<html>
<body>
<div id="dowhateveryouwant">
<?php
while($data = mysql_fetch_array($result)) // Yes, a single '='. This lets you loop through all rows returned to you
{
echo "<p> The data is:" . $data['username'] . " " . $data['tableColumnName'] . " etc...";
}
echo "</p>";
?>
</div>
</body>
</html>
<?php
// Step 3 complete.
mysql_close($db);
// Step 4 complete.
?>
Hope that helps.

Re: Database Question
Posted: Fri Dec 21, 2007 4:43 pm
by califdon
justinsl wrote:I didnt mean to sound as if I was not wanting to spend the time to learn. Let me ask you this.
Tell me if I am on the right track.
Would I go about it in this order?
1. Connect to the Database
2. Ask the database to find the row with "productnamegoeshere"
2. Display the fields in the row on the page
4 Close the database
So if I call that row I can then echo them into the page?
Yes, you've got the right steps (other than you don't usually need to explicitly close the database, the connection disappears when the script ends).
I am assuming that I can learn how to (providing PHP can which im sure it can) have the code take the name of the page itself and use it as a var to query the database. Like "product2.php" the page will pull its own name in as a var and use it to query the database where I might have a field called "pagename"
Now here you're confusing me a bit. The database doesn't care about the name of the page that queries it. Your connection string will include the name of the server or host, a valid user for the database, and the password for that user. So the name of the script is not involved at all. Then you will select a database (there are 2 steps: connecting to the host and selecting the database) and send it the query, expressed in SQL. The syntax of the query request assigns the resulting rows (assuming there are some) to an object, from which, in PHP, you can then fetch the rows in a loop. Each of these steps has variations, but fundamentally it's like:
Code: Select all
mysql_connect("localhost","root","abracadabra") or die("Couldn't connect to database!");
mysql_select_db("mystuff") or die("Couldn't find database!");
$sqlstr = "SELECT * FROM mytable WHERE mynumber = $myvar";
$result = mysql_query($sqlstr);
while ($row = mysql_fetch_array($result)) {
echo "Name: $row[0], Number: $row[1]<br />";
}
Ill have to look it up but my site layout is 100% CSS, crossbrowser and validates im wondering if php is going to toss a wrench into it?
Nah! PHP only preprocesses stuff at the server before sending it to the browser. As long as you don't
generate nonstandard HTML in the process of sending data to the browser, it has no way of knowing that it isn't coming from a static HTML file. It's really very cool.
Re: Database Question
Posted: Fri Dec 21, 2007 4:52 pm
by Chalks
califdon wrote:It's really [s]very cool[/s] SUPER FREAKING SWEET.
That's what I saw.
