MySQL数组中的MySQL结果问题

时间:2013-01-18 02:16:25

标签: php sql

有人可以告诉我为什么MySQL的所有结果都没有在数组中结束?

$result = mysql_query("select * from groups order by id desc");

if ($row = $result->fetch()) {
$groups[] = $row; 
}

3 个答案:

答案 0 :(得分:3)

使用while而不是if

while ($row = $result->fetch()) {
  $groups[] = $row; 
}

答案 1 :(得分:2)

您在那里的代码不会迭代结果集。 试试这个。

while ($row = $result->fetch()) { 
$groups[] = $row;
}

答案 2 :(得分:0)

因为fetch只按php manual

中的说明提取行
  

从结果集中获取下一行

我想建议更改PDO的mysql_代码

$db = new PDO("..."); // Creates the PDO object. Put the right arguments for your connection.
$statement = $db->prepare("SELECT * FROM groups ORDER BY id DESC");
$statement->execute();
while ($groups = $statement->fetch())
  {
  // Do whatever you want to do

  }