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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失