2011-03-07 105 views
4
$query = "SELECT * FROM table"; 
$result = mysql_query($query, $db); 
$all = mysql_fetch_assoc($result); 
echo mysql_num_rows($result) . ":" . count($all); 

這將返回mysql_fetch_array不檢索所有行

2063:7 

我以前沒有使用過算的,所以我不是100%肯定它不是計數表列。這很晚了,我可能會瘋了。

這裏是發生了什麼事的另一個例子:

$result = mysql_query($query, $db); 
echo "Rows: " . mysql_num_rows($result) . " <BR />"; 

$player_array = mysql_fetch_assoc($result); 
echo "<pre>"; 
print_r($player_array); 
echo "</pre>"; 

,輸出:

Rows: 9 
Array 
(
    [playerID] => 10000030 
) 

TL; DR:我提交其返回多行查詢,但fetch_array只給我那些小部分結果數組中的行。

回答

5

mysql_fetch_assoc只返回一個在一旦你必須使用循環來檢索所有行

while($row = mysql_fetch_assoc($result)) 
{ 
    print_r($row); 
} 
7

mysql_fetch_assoc($result);每次調用給你一個結果集的行:

from the documentation

mysql_fetch_assoc - 獲取結果行a s關聯數組

返回與獲取的行對應的關聯數組,並將內部數據指針向前移動。對於可選的第二個參數,mysql_fetch_assoc()等效於調用mysql_fetch_array()和MYSQL_ASSOC。它只返回一個關聯數組。

你必須使用的函數在一個循環:

$all = array(); 

while(($row = mysql_fetch_assoc($result))) { 
    $all[] = $row; 
} 

The example in the document shows how it is done

+1

我很抱歉做一個笨蛋。這是一個巨大的調試的尾端,我多年以來一直沒有使用PHP。這是我的故事! – sejje 2011-03-07 10:04:42

+0

@sejje:不需要道歉:) – 2011-03-07 10:06:39

2

mysql_fetch_assoc不能以這種方式工作,您需要多次調用以獲取所有行。像這樣:

while ($row = mysql_fetch_assoc($db_result)) 
{ 
    print_r($row); 
}