我有一个php网页,我希望它能显示所有的论坛线程在一次。他是我目前拥有的数据库代码,它只在线程上显示。
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 />';如何让它输出这个表中的每一条记录?
谢谢。
发布于 2011-08-09 03:26:13
你需要使用一个while循环。fetch函数一次只获取一行。
while($row = mysql_fetch_array($result)) {
echo ...
}发布于 2011-08-09 03:26:19
你只需要抓取第一条记录,循环遍历每条记录:
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 />';
}发布于 2011-08-09 03:27:32
使用循环:
$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的示例部分)
https://stackoverflow.com/questions/6987437
复制相似问题