mysql_fetch_array不检索所有行

时间:2011-03-07 09:45:05

标签: php mysql arrays

$query = "SELECT * FROM table";
$result = mysql_query($query, $db);
$all = mysql_fetch_assoc($result);
echo mysql_num_rows($result) . ":" . count($all);

返回

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只给出了结果数组中这些行的一小部分。

3 个答案:

答案 0 :(得分:7)

一旦你必须使用循环来检索所有行

mysql_fetch_assoc只返回一行

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

答案 1 :(得分:7)

每次拨打mysql_fetch_assoc($result);都会为您提供一行的结果集:

from the documentation

  

mysql_fetch_assoc - 将结果行作为关联数组获取

     

返回与获取的行对应的关联数组,并将内部数据指针向前移动。 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

答案 2 :(得分:2)

mysql_fetch_assoc无法正常工作,需要多次调用才能获取所有行。像这样:

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