MySQL结果问题在php数组


MySQL results issue in php array

谁能告诉我为什么不是所有的结果MySQL不是结束在数组?

$result = mysql_query("select * from groups order by id desc");
if ($row = $result->fetch()) {
$groups[] = $row; 
}

使用while而不是if

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

这里的代码不会遍历结果集。

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

因为fetch只获取一行,正如php手册中解释的那样:

从结果集

中获取下一行

我想建议改变你的mysql_代码为PDO

$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
  }