0

请告诉,为什么这个代码是错误的?

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];
}

PS此代码正在运行:

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

1 回答 1

2

myres()每次都来这里,我想:

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

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

$res = myres();

while ($row = $res->fetch_row()) {
  echo  $row[0];
}
于 2012-07-28T16:00:16.353 回答