hayhay
hayhay

Reputation: 73

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);

Upvotes: 0

Views: 169

Answers (2)

Bitwise Creative
Bitwise Creative

Reputation: 4105

UPDATE: My original answer was starkly incorrect for this question. date is not a reserved word and can be used without quoting (thanks for the schooling, guys). However, because unquoted reserved words can be a common issue which could result in the same error, I'm leaving this up for future readers in case it helps (https://meta.stackexchange.com/questions/37738/when-or-should-you-delete-your-incorrect-answer). Basically:

Check your column names. You may have unquoted reserved words.

https://dev.mysql.com/doc/refman/5.5/en/keywords.html


ORIGINAL ANSWER:

You need to quote your column names with backticks. Your date field is a reserved word.

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

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Upvotes: 2

RiggsFolly
RiggsFolly

Reputation: 94682

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);

Upvotes: 0

Related Questions