hayhay hayhay - 7 months ago 11
PHP Question

PHP Prepared statement bind_param() error

Many people on stackoverflow has had this problem, but i still cannot spot the mistake

This is the error:

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


This is the lines of code:

$insertpost = $conn->prepare("INSERT INTO posts (title,post,user,img,date,short) VALUES(?,?,?,?,NOW(),?)");
$insertpost->bind_param("sssss",$title,$comment,$user,$url,$short);

Answer

After every mysqli or PDO function that COULD return a bad/failed status you must test for that possibility.

The error Fatal error: Call to a member function bind_param() on boolean says it all. $insertpost is false so the prepare failed for SOME reason, it could be a bad query or it could be that the MYSQL Server has crashed and cannot prepare the statement.

So you must code accordingly

$insertpost = $conn->prepare("INSERT INTO posts 
                                    (title,post,user,img,date,short) 
                              VALUES(?,?,?,?,NOW(),?)");

if ( $insertpost === FALSE ) {
    // prepare failed for some reason, lets see why
    echo $conn->error;
    exit;
}
$insertpost->bind_param("sssss",$title,$comment,$user,$url,$short);