2011-04-27 83 views
1

我正在嘗試使用以下代碼顯示我的表CarCollection中的所有記錄。現在我只能返回第一條記錄。我怎麼能做到這一點?使用php顯示MySQL數據

$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); 
+2

你需要使用一個while循環。 while($ row = mysql_fetch_assoc($ result)) – 2011-04-27 05:56:23

回答

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); 
?> 
+0

歡迎bro – 2011-04-28 02:48:39

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); 
?> 
0

關閉後的mysql_query MySQL連接好編程實踐。

+0

是不是正確?它的最後一行代碼。 'mysql_close($連接);' – jack 2011-04-27 15:10:25

0

將一個while循環添加到mysql_fetch_array();這就是它...