dp518158 2014-08-27 08:21
浏览 77
已采纳

PHP mysqli_fetch_array应该得到一个布尔值

$result = mysqli_query($conn,"SELECT * FROM Players");
if ($result !== FALSE) {
    while($row = mysqli_fetch_array($result)) {
        $result = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
    }
}

This works. That is, the databse is indeed updated and everything is all cool.

But it throws a warning:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

If you search around, the explanation is that the query is failing - hence, it is returning FALSE, so you get the warning.

... But this doesn't make sense in my case. The query is not failing. When I run this script, my database is updated just fine. Besides, there is also a conditional checking if the result is a boolean before using mysqli_fetch_array, so technically this warning should never happen in the first place.

Whatever, the problem must be with $result. Let's do:

echo gettype($result);

Which results in

"object"

Well, this explains why is it passing the condition. However, this still won't explain why mysqli_fetch_array insists this is a boolean (because it isn't).

What is the problem, then?

Tested with PHP Version 5.3.24 and 5.4.19.

  • 写回答

3条回答 默认 最新

  • dsaf32131 2014-08-27 08:23
    关注

    You're overwriting the resultset $result from your SELECT query with a new $result value from the UPDATE query inside your loop

    while($row = mysqli_fetch_array($result)) {
        $result2 = mysqli_query($conn,"UPDATE Players SET Score='$score' WHERE ID='$id'");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器