Retrieving Values from MySQL with PHP.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
honkyinc
Forum Newbie
Posts: 19
Joined: Tue Jun 04, 2002 10:30 am
Location: Maryland, USA

Retrieving Values from MySQL with PHP.

Post 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???)
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
Post Reply