duanshan7261 2019-08-14 05:51
浏览 159
已采纳

查询返回结果但是给出了关于bolean的警告

I am trying to upgrade an old application to PHP 7.2. It contains an sql class PHP file with the following function which I have modified to use mysqli:

    function query($query, $index=0)
    {
        // query
        if (!$this->res[$index] = mysqli_query($this->connection, $query))
        {
            // if query fails show error
            $this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
            return false;
        }

        // statistical information
        $this->num_rows[$index] = @mysqli_num_rows($this->res[$index]);
        $this->num_flds[$index] = @mysqli_num_fields($this->res[$index]);
        $this->num_aff[$index]  = @mysqli_affected_rows($this->connection);
        $this->last_id             = @mysqli_insert_id($this->connection);

        return true;
    }

This function throws the folling error:

E_WARNING Error in file �sql.class.php� at line 132: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given E_WARNING Error in file �sql.class.php� at line 133: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given

My initial thought was that the query was failing. However, including this line inside the function...

print_r(mysqli_fetch_assoc($this->res[$index]));

results in the following output:

Array ( [s_id] => 2088b4cc0d026c2742e8e0cb7d7c8e95 )

In the output above, the query is returning a session ID. That leaves me a bit confused because the value of $this->res[$index] is not a bolean, yet the Warning says it is.

Edit:

If I include this in the function:

        echo mysqli_num_rows($this->res[$index]);
        echo mysqli_num_fields($this->res[$index]);

Each line echos the correct value of 1 but each line also produces the boolean Warning...

E_WARNING Error in file �sql.class.php� at line 125: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
E_WARNING Error in file �sql.class.php� at line 126: mysqli_num_fields() expects parameter 1 to be mysqli_result, boolean given
  • 写回答

1条回答 默认 最新

  • doufeng9567 2019-08-14 08:27
    关注

    mysqli_query return values : If can be mysqli_result object,true, false

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

    try this modification to see which query really successful but result was true

    function query($query, $index=0)
    {
        $this->res[$index] = mysqli_query($this->connection, $query);
        // query
        if (is_bool($this->res[$index])) {
            if ($this->res[$index] === false)
            {
                // if query fails show error
                $this->error('<strong>invalid query</strong>:<br />' . $query . '<br />');
                return false;
            } else {
                // query was successful, but the result is not a mysqli_result object
                $this->warning('<strong>success with no returned data query</strong>:<br />' . $query . '<br />');
                return true;
            }
        }
    
    
        // statistical information
        $this->num_rows[$index] = @mysqli_num_rows($this->res[$index]);
        $this->num_flds[$index] = @mysqli_num_fields($this->res[$index]);
        $this->num_aff[$index]  = @mysqli_affected_rows($this->connection);
        $this->last_id             = @mysqli_insert_id($this->connection);
    
        return true;
    }
    

    Check that $this->warning is exist in your code, or update it to what it will be proper

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP