Show muliple records from mysql
I have a php webpage that I want it to show all forum threads at once. He is the current db code i have, it only shows on thread.
mysql_connect("localhost", "", "") or die("could not connect to mysql");
mysql_select_db("") or die("could not connect to db");
$result = mysql_query("SELECT * FROM reference")
or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "<strong>ref_thread_id:</strong> ".$row['ref_thread_id'].'<br />';
echo "<strong>ref_thread_prefix:</strong> ".$row['ref_thread_prefix'].'<br />';
echo "<strong>ref_thread_topic:</strong> ".$row['ref_thre开发者_JS百科ad_topic'].'<br />';
echo "<strong>ref_thread_content:</strong> ".$row['ref_thread_content'].'<br />';
How do I get it to spit out every record in this table?
Thanks.
You'd need to use a while loop. the fetch functions only get one row at a time.
while($row = mysql_fetch_array($result)) {
echo ...
}
You're only grabbing the first record, loop through each one:
while ( $row = mysql_fetch_array( $result )) {
echo "<strong>ref_thread_id:</strong> ".$row['ref_thread_id'].'<br />';
echo "<strong>ref_thread_prefix:</strong> ".$row['ref_thread_prefix'].'<br />';
echo "<strong>ref_thread_topic:</strong> ".$row['ref_thread_topic'].'<br />';
echo "<strong>ref_thread_content:</strong> ".$row['ref_thread_content'].'<br />';
}
Use a loop:
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
(Example part taken from http://php.net/manual/en/function.mysql-fetch-array.php)
精彩评论