我有一个php网页,我希望它能够立即显示所有论坛帖子。他是我现有的db代码,它只在线程上显示。
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_thread_topic'].'<br />';
echo "<strong>ref_thread_content:</strong> ".$row['ref_thread_content'].'<br />';
如何让它吐出此表中的每条记录?
感谢。
答案 0 :(得分:2)
您需要使用while循环。获取函数一次只能获得一行。
while($row = mysql_fetch_array($result)) {
echo ...
}
答案 1 :(得分:1)
你只抓住第一条记录,遍历每条记录:
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 />';
}
答案 2 :(得分:0)
使用循环:
$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]);
}
(示例部分取自http://php.net/manual/en/function.mysql-fetch-array.php)