[英]Displaying MySQL data with php
I am trying to display all records from my table CarCollection
using the following code. 我正在尝试使用以下代码显示表
CarCollection
中的所有记录。 Right now I am only able to return the 1st record. 目前,我只能返回第一条记录。 how can I achieve this?
我该如何实现?
$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);
<?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);
?>
Above is correct - I normally have another part to the while loop to make sure that the result is still set: 以上是正确的-我通常在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);
?>
mysql_query是一种好的编程习惯之后,关闭mysql连接。
Add a while loop to the mysql_fetch_array(); 向mysql_fetch_array()添加一个while循环; thats it...
而已...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.