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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀