2012-03-18 150 views
1

我知道我的表中有兩行數據符合where子句的條件,但我的編碼只輸出(回顯)一行。我究竟做錯了什麼?MySQL SELECT語句只返回一行

<?php 


$connect= mysqli_connect("localhost", "root", "", "friends_list") 
or die('error connecting with the database'); 

$query= "SELECT * FROM people WHERE age=19"; 

$result= mysqli_query($connect, $query) 
or die('error querying the database'); 

$row= mysqli_fetch_array($result); 

while ($row = mysqli_fetch_array($result)) 
{ 
echo $row['first_name'] . " " . $row['last_name'] . " is " . $row['age'] . "<br/>"        
} 
mysqli_close($connect); 

?> 

回答

0

您正在提取第一行,但沒有對它做任何操作,所以此行不會在while循環內被提取,因此不會被輸出。

$row= mysqli_fetch_array($result); // remove this 

while ($row = mysqli_fetch_array($result)) 
{ 
echo $row['first_name'] . " " . $row['last_name'] . " is " . $row['age'] . "<br/>"        
} 
5

你在吞嚥第一行,並且沒有做任何事情;即改變:

$row= mysqli_fetch_array($result); 

while ($row = mysqli_fetch_array($result)) 

到:

while ($row = mysqli_fetch_array($result)) 
0

在這一部分:

$row= mysqli_fetch_array($result); 

while ($row = mysqli_fetch_array($result)) 
{ 

你跳過第一行。你抓取它,然後在while循環中立即獲取下一個。您應該刪除第一個提取並且僅限於此:

while ($row = mysqli_fetch_array($result)) 
{