使用用户函数从mysql返回结果

时间:2012-07-28 15:56:36

标签: php arrays loops

请告诉我,为什么这段代码错了?

function myres () {
   $db = new mysqli("localhost","userrr","pass","mvc");
   $res = $db->query("SELECT * FROM news ");
   return $res;
}


while ($row = myres()->fetch_row()) {
    echo  $row[0];
}

P.S。 这段代码正在运行:

$db = new mysqli("localhost","userrr","pass","mvc");
$res = $db->query("SELECT * FROM news ");
while ($row = $res->fetch_row()) {
    echo  $row[0];
}

1 个答案:

答案 0 :(得分:2)

我每次都打电话给myres(),我想:

while ($row = myres()->fetch_row()) {
  echo  $row[0];
}

所以每次$row包含结果的第一行,它都不会停止。我认为它会正常工作:

$res = myres();

while ($row = $res->fetch_row()) {
  echo  $row[0];
}