0

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);
6
  • 2
    $insertpost might be bool false, probably because the prepare() fails. Commented Apr 24, 2016 at 17:03
  • Possible duplicate of mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given Commented Apr 24, 2016 at 17:04
  • You are defining all strings as parameter types, "sssss", looking at your parameters, you seem to have types other than the ones you are defining. I can at least see possible: date/datetime and integer types. It's just a suggestion, they may be implicit conversions, but I usually prefer to type things correctly regardless of if they have implicit conversions, just to be safe. Commented Apr 24, 2016 at 17:05
  • Basically the query FAILED therefore the variable $insertpost contains false If you test for this situation ALWAYS you wont get this problem. Aso use $conn->error to see a description of the error if $insertpost === false Commented Apr 24, 2016 at 17:09
  • $insertpost = "INSERT INTO posts (title,post,user,img,date,short) VALUES('".$title."','".$comment."','".$user."','".$url."','".$date."','".$short."')"; I know it works because this worked previously... Commented Apr 24, 2016 at 17:10

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

9 Comments

Me using date has nothing to do with it, and encapsulating also does nothing in this case.
While date is a bad name to give a column MYSQL made a special case of it so it does not actually throw an error any more and does not caiuse queries to fail. But you should not use it as a column name
I've used this in other places in my code, it works this way - $posts = $conn->prepare("SELECT id,title,post,short,user,img,date FROM posts ORDER BY posts.date DESC LIMIT ?"); $posts->bind_param("s",$limit);
Well, it does though. prepare fails because the query is invalid when you don't encapsulate a reserved word field name...
date is not a reserved word, it's a keyword; two different animals here. Have look for yourself dev.mysql.com/doc/refman/5.5/en/keywords.html there's no (R) next to it. Let's lose that misconception about that ;-) You even included that link in your answer.
|
0

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

1 Comment

Well, thanks. I added that and got the error - "Commands out of sync; you can't run this command now", so i realised i had to close my previous connections. Then i got the error - "Php mysqi bind_param Number of variables doesn't match number of parameters in prepared statement" So i had to get rid of the ' ' the previous guy told me to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.