PHP警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在.....中使用SELECT MAX给出的字符串

aln*_*447 0 php mysql

我知道在论坛上有数百个这样的帖子,但似乎没有一个可以帮助我解决问题.这是PHP代码:

  $countsql = "SELECT MAX(id) as count FROM art";
  $count = mysqli_query($link, $countsql);
  if(!$count){
    echo "Error\n";
    echo "Code: ". mysqli_error($link);
    exit;
  }

while ($row = mysqli_fetch_assoc($count)){ $count = $row["count"]; }

  $ppp = 2;
  $start = $count - (($page-1) * $ppp);
  $end = $count - (($page-1) * 2 * $ppp);
Run Code Online (Sandbox Code Playgroud)

问题出现在mysqli_fetch_assoc的行中.任何帮助都将很高兴.

Bar*_*chs 5

你的问题在这里:

while ($row = mysqli_fetch_assoc($count)){ 
   $count = $row["count"]; 
}
Run Code Online (Sandbox Code Playgroud)

第二次迭代设置$count为字符串,使mysqli_fetch_assoc调用失败.

因为结果集中只有一行,所以可以更改whileif:

if ($row = mysqli_fetch_assoc($count)){ 
   $count = $row["count"]; 
}
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,不要将相同的变量用于两个(非常不同的)目的.

$count_result = mysqli_query($link, $countsql);
...
if ($row = mysqli_fetch_assoc($count_result)){ 
   $count = $row["count"]; 
}
Run Code Online (Sandbox Code Playgroud)