Php 显示mysql中的多条记录
我有一个php网页,我希望它显示所有论坛线程一次。他是我目前的数据库代码,它只显示在线程上Php 显示mysql中的多条记录,php,database,Php,Database,我有一个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());
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 />';
mysql\u connect(“localhost”)、“”、“”)或die(“无法连接到mysql”);
mysql_select_db(“”)或die(“无法连接到db”);
$result=mysql\u查询(“从引用中选择*)
或者死(mysql_error());
$row=mysql\u fetch\u数组($result);
echo“ref\u thread\u id:”$row['ref\u thread\u id']”。
;
echo“参考线程前缀:”$row['ref\u thread\u prefix']”。
;
echo“参考线程主题:”$row['ref\u thread\u topic'].
;
echo“参考线程内容:”$row['ref\u thread\u content']”。
;
我如何让它吐出这张表中的所有记录
谢谢。您需要使用while循环。fetch函数一次只能获取一行
while($row = mysql_fetch_array($result)) {
echo ...
}
您只需抓取第一条记录,循环浏览每一条记录:
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 />';
}
while($row=mysql\u fetch\u array($result)){
echo“ref\u thread\u id:”$row['ref\u thread\u id']”。
;
echo“参考线程前缀:”$row['ref\u thread\u prefix']”。
;
echo“参考线程主题:”$row['ref\u thread\u topic'].
;
echo“参考线程内容:”$row['ref\u thread\u content']”。
;
}
使用循环:
$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]);
}
(示例部分摘自)