PHP Fatal error: Call to a member function fetch_object() on boolean

后端 未结 2 465
孤独总比滥情好
孤独总比滥情好 2020-12-11 12:28

I got php fatal error after transfer server with php v5.6.19, before that I had no problem at all with following script

Fetch data from db table:

2条回答
  •  天命终不由人
    2020-12-11 13:14

    First, you are returning a boolean from your function. So, no wonder PHP says you so.

    Second, you should keep the matters separated. a function that works with mysqli should keep all mysqli stuff inside. An return just an array, that can be used anywhere without the need to call mysqli functions again.

    function get_department_list($mysqli)
    {
        $sql = $mysqli->query("SELECT * FROM `dept` ORDER BY `dept_id` ASC");
        return $sql->fetch_all();
    }
    

    And then use not while but foreach

    foreach ($depts as $dept) ...
    

    Besides (and more for the people who may chance to land on this question looking for an answer to their question) you should always set proper error reporting for mysqli, like it shown in this answer

提交回复
热议问题