Web Analytics Made Easy -
StatCounter Call to a member function fetch_array - CodingForum


No announcement yet.

Call to a member function fetch_array

  • Filter
  • Time
  • Show
Clear All
new posts

  • Call to a member function fetch_array

    I am very new to php and I have no idea what does this error means
    Call to a member function fetch_array() on boolean

    this is my code.How can I fix it

    $db=new mysqli("localhost","root","");
    $result=$db->query("select * from okul");

  • #2
    Rather than someone telling you what to do to fix the error, how about debugging the problem to find what's causing the error.

    This is the line where the error is occurring (assuming you have posted the correct code) - $row=$result->fetch_array();

    Wouldn't that particular error mean that $result is a Boolean value, which is not what you and your code is expecting it to be?

    Where does $result get its value? From the ->query() method call. If you read the php.net documentation for the mysqli->query() method, you will find this bit of information -

    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.
    So, it means that the ->query() call failed.

    If you add some error handling for the mysqli statements (your code should ALWAYS have error handling in it anyway, not just for debugging problems), you can get php/mysql to tell you why the statements are failing. The easiest way of handling database statement errors is to use exceptions. To enable exceptions for the mysqli statements, add the following to your script (if you add it before the connection/new mysqli() statement it will also handled connection errors) -

    PHP Code:
    $driver = new mysqli_driver();
    $driver->report_mode MYSQLI_REPORT_ALL;
    // note this will also throw errors if your query isn't using an index, so you may want to use - MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT instead. 
    Next, if php's error_reporting is set to E_ALL and display_errors as set to ON, php will report and display the mysqli statement errors as part of its handling of the exception that will be thrown.
    Last edited by CFMaBiSmAd; Sep 17, 2016, 01:46 PM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


    • #3
      Tank you so much