开发者

Displaying MySQL data with php

开发者 https://www.devze.com 2023-02-28 19:16 出处:网络
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?

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消