Uncaught Error: Call to a member function prepare() on bool

Hello guys Im trying to acces the db and pick up some data and transfer it o JSON file, Im veeeeery new to php and i struggle last two days with this error.

This is a error report from postman


mysqli:
Pripojeno, server 5.6.33-79.0-log

Fatal error: Uncaught Error: Call to a member function prepare() on bool in
api_spolu/Post.php: 39
Stack trace:
#0api_spolu/read.php(23): Post->read()
#1 {main
}
thrown in api_spolu/Post.php on line 39

I use 3 files to do this :

1.st is ok - the connection to db

2,3.

I’ve tried everything now, any ideas ? Im really lost… And if yes please note that im total newbie so be kind with explanation, thank you :smiley: <3

 <?php
   include_once  "Post.php";
   include_once "Database.php";  
    // Headers
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');

    // databaza - connect
    $database = new Mysqli();
    $db =  $database->connect();

    // Post objekt
    $post = new Post($db);

    // Post query
    $result = $post->read();

    //Get rows
    $num = $result->rowCount();

    //su tu posty ?
    if($num > 0) {
        //Post pole
        $post_pole = array();
        $post_pole['data'] = array();

        while($row = $result->fetch(PDO::FETCH_ASSOC)) {
            extract($row);

            $post_item = array(
                'id' => $id,
                'title' => $title,
                'body' => html_entity_decode($body),
                'time' => $time,
            );
            
            //Daj do pola data
            array_push($post_pole['data'], $post_item);
        }

        //Premen do JSONU
        echo json_encode($post_pole);

    } else {
        //Niesu posty
        echo json_encode(
            array('message' => 'No post Found')
        );
    }

The error you are getting is a follow-on error, having nothing directly to do with the actual problem. It’s because the posted connection code has failed, but there’s no error handling to stop program execution. The code continues to run past the point where the problem is at and finally produces a fatal runtime error when something is called that’s dependent on a variable holding a valid connection. You also apparently don’t have php’s error_reporting set to E_ALL. The posted connection code would be producing a php warning message.

You ALWAYS need error handling for statements that can fail. For database statements, the easiest way of adding error handling is to simply use exceptions for errors and in most cases let php catch and handle the exception, where it will use its error related settings to control what happens with the actual error information.

To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Doing this will at least stop code execution at the point where the first database statement is failing.

So, what is wrong with the posted connection code -

    $database = new Mysqli();
    $db =  $database->connect();

This is redundant and is misusing the value from the ->connect() method call. You would normally just use the first line of code, either supplying the connection credentials as call-time parameters or setting them as the default connection values in a php.ini/htaccess file. If you did have a case where you used the first line to just create an instance of the mysqli class, without making a connection, then call the ->connect() method, the ->connect() method doesn’t return the connection. It just makes the connection, which would be available via the $database variable. Despite the documentation for the ->connect() method, it returns a false if the connection fails, and returns void/null if the connection works. This is where the boolean false value is coming from in the prepare() method call. $db is not the connection. $database would be the connection.

Short-answer: 1) ALWAYS have error handling for statements that can fail, 2) make sure php’s error_reporting is ALWAYS set to E_ALL, and 3) use the simplest code to accomplish a task. You only need one line of code to make a database connection.

Sponsor our Newsletter | Privacy Policy | Terms of Service