我正在尝试使用以下代码显示表CarCollection
中的所有记录。现在我只能返回第一条记录。我怎么能实现这个目标呢?
$connection = mysql_connect("localhost","USER_NAME","PASSWORD");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE_NAME", $connection);
$result = mysql_query("SELECT * FROM CarCollection");
$row = mysql_fetch_array($result);
mysql_close($connection);
答案 0 :(得分:6)
<?php
$connection = mysql_connect("localhost","USER_NAME","PASSWORD");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE_NAME", $connection);
$result = mysql_query("SELECT * FROM CarCollection");
while($row = mysql_fetch_array($result)){
echo $row[0];
echo $row[1];
}
mysql_close($connection);
?>
答案 1 :(得分:1)
以上是正确的 - 我通常在while循环中有另一部分,以确保结果仍然设置:
<?php
$connection = mysql_connect("localhost","USER_NAME","PASSWORD");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE_NAME", $connection);
$result = mysql_query("SELECT * FROM CarCollection");
while($result && $row = mysql_fetch_array($result)){
echo $row[0];
echo $row[1];
//Or You can Name the Columns
echo $row['name'];
}
mysql_close($connection);
?>
答案 2 :(得分:0)
mysql_query 是一个很好的编程习惯后关闭mysql连接。
答案 3 :(得分:0)
将一个while循环添加到mysql_fetch_array();多数民众赞成......