I am trying to display all records from my table CarCollection
using the following code. 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:
<?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);
?>
Close the mysql connection after mysql_query is good programming practice.
Add a while loop to the mysql_fetch_array(); thats it...
精彩评论