How do I display the query results in a table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Winter
Forum Newbie
Posts: 15
Joined: Fri Feb 07, 2003 12:29 pm
Location: Texas
Contact:

How do I display the query results in a table?

Post by Winter »

Hello All,

I have just learned PHP just for one week. Here is a problem that I cannot find the answer from my reference books. “How do I select the data from MySQL database and display the results in a table?”

Thanks for your help!!


<?
$connection=mysql_connect("localhost", "root", "") or die("couldn't connect");
$db=mysql_select_db("questionnairedb", $connection) or die("couldn't connect to database!");
$sql="select ID, District from testdb order by ID";
$result=mysql_query($sql,$connection) or die("couldn't execute the query");

$hm="<table border=1>";
for ($i=0; $i<mysql_num_rows($result); $i++){
$hm.="<tr><td>$ID</td><td>$District</td></tr>";
}
$hm.="</table>";

?>
<html>
<body>
<?
echo "$hm";
?>
</body>
</html>

Winter :oops:
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post by skehoe »

I would try replacing:
$hm="<table border=1>";
for ($i=0; $i<mysql_num_rows($result); $i++){
$hm.="<tr><td>$ID</td><td>$District</td></tr>";
}
$hm.="</table>";
With this:

Code: Select all

$hm = "<table border='1'>";
while(list($ID, $District) = mysql_fetch_row($result)) &#123;
    $hm .= "<tr><td>$ID</td><td>$District</td></tr>"; 
&#125;
$hm .= "</table>";
Hope that helps!
Winter
Forum Newbie
Posts: 15
Joined: Fri Feb 07, 2003 12:29 pm
Location: Texas
Contact:

Post by Winter »

It works! Thank you so much!


Winter :lol:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

So far I just been building up tables a row at a time with .= but I just noticed this: ovrimos_result_all (see manual).

Haven't tried it yet.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

unfortunatly this function is only available for the commercial Ovrimos SQL Server
http://www.php.net/manual/en/ref.ovrimos.php
Post Reply