Page 1 of 1

Retrieving Values from MySQL with PHP.

Posted: Tue Jun 04, 2002 10:30 am
by honkyinc
Hi all!

I am trying to learn all this database stuff (just started), and so far I've been able to connect to my database, create a table and insert data into that table using a form. The table only has two columns, firstName and lastName (test db, hehe).

What I'd like to know is how I would extract each row from the DB and output it to the window in a nice, orderly fashion? I believe this might require a for loop to get the values and print them out, but I'm not sure.

Thanks in advance, and I'm lovin' PHP Databases! (crowd roars: Are you nuts???)

Posted: Tue Jun 04, 2002 1:18 pm
by mikeq
//connect to the database
$Conn = MySQL_Connect($Server,$User,$Password);

//select the database
MySQL_Select_DB($Database);

//construct your query
$QueryStr = "select * from mytable";

//run the query
$Result = MySQL_Query($QueryStr);

//use a while loop to iterate through result set
//MySQL_Fetch_Array will return false when no more records to fetch
while ($Row=MySQL_Fetch_Array($Result)){
$FirstName = $Row['FIRSTNAME'];
$LastName = $Row['LASTNAME'];
print "<table>";
print "<tr><td>$FirstName</td><td>$LastName</td></tr>";
print "</table>";
}

I have omitted all the other HTML output, be careful with the case in $Row['FIRSTNAME'] I have found it may not return anything if you use the wrong case, make sure it is the same as the case you created the table with.

Mike