Gavin
Gavin

Reputation: 2824

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

<?php
    session_start();
    $config = parse_ini_file('../database_config.ini'); 
    //Create Database connection
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

    if ($connection->connect_error) {
        die('Could not connect to db: ' . mysql_error());
    }


    $stmt = $connection->prepare("INSERT INTO report (reportID, userID, description, address, postalcode, latitude, longitude) 
    VALUES(0, 007, 'Major fire', 'Jurong Point', 640724, 1.640724, 103.640724)");

        $stmt->execute();
        echo "Error:\n";
        print_r($stmt->error_list);
        $stmt->close();

        $connection->close();
?>

Error : Fatal error: Call to a member function execute() on boolean

Why does my prepare statement fail?

Structure of my report table enter image description here

Upvotes: 0

Views: 4414

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to do it in following manner:-

<?php
    session_start();
    $config = parse_ini_file('../database_config.ini'); 
    //Create Database connection
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

    if (mysqli_connect_errno()) { // check the change of if condition
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = mysqli_prepare($connection,"INSERT INTO report (reportID, userID, description, address, postalcode, latitude, longitude) VALUES(0, 007, 'Major fire', 'Jurong Point', 640724, 1.640724, 103.640724)"); // check the change in query code

    mysqli_stmt_execute($stmt); // check the change in execution code
    echo "Error:\n";
    print_r(mysqli_error($connection)); // check the change in error getting code
    mysqli_stmt_close($stmt);// check the change in statement closing code
    mysqli_close($connection); // check the change in db connection closing code
?>

For more knowledge refer link and it's example:- http://php.net/manual/en/mysqli.prepare.php

Upvotes: 1

Jordy
Jordy

Reputation: 1049

You are combining Object Oriented style with the normal procedural mysqli style. On line 5 you use.

mysqli_connect()

and on line 12 you use.

$connection->prepare()

This will not work, if you'd change $connection to, object oriented style like you do with your prepare statement, it will work.

$connection = new mysqli('localhost', $config['username'], $config['password'], $config['dbname'])

More information can be found here http://php.net/manual/en/mysqli.prepare.php

Upvotes: 2

Related Questions