Page 1 of 1

How do I display the query results in a table?

Posted: Tue Feb 11, 2003 5:03 pm
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:

Posted: Tue Feb 11, 2003 5:57 pm
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!

Posted: Wed Feb 12, 2003 10:59 am
by Winter
It works! Thank you so much!


Winter :lol:

Posted: Wed Feb 12, 2003 2:52 pm
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.

Posted: Wed Feb 12, 2003 3:02 pm
by volka
unfortunatly this function is only available for the commercial Ovrimos SQL Server
http://www.php.net/manual/en/ref.ovrimos.php