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

后端 未结 2 463
孤独总比滥情好
孤独总比滥情好 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

    0 讨论(0)
  • 2020-12-11 13:25

    Update your while loop for that case when you get false from $shop->get_department_list() call

    updated while like this check for $depts if any data then get $dept:

    while($depts && $dept = $depts->fetch_object()){
    
    0 讨论(0)
提交回复
热议问题