简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM