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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
hayhay
  • 73
  • 1
  • 11
  • 2
    `$insertpost` might be bool `false`, probably because the `prepare()` fails. – frz3993 Apr 24 '16 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](http://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-expects-parameter-1-to) – alexander.polomodov Apr 24 '16 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. – gmiley Apr 24 '16 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` – RiggsFolly Apr 24 '16 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... – hayhay Apr 24 '16 at 17:10
  • What has changed between now and when it last worked? – gmiley Apr 24 '16 at 17:18

2 Answers2

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

Community
  • 1
  • 1
Bitwise Creative
  • 4,035
  • 5
  • 27
  • 35
  • Me using date has nothing to do with it, and encapsulating also does nothing in this case. – hayhay Apr 24 '16 at 17:10
  • 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** – RiggsFolly Apr 24 '16 at 17:12
  • 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); – hayhay Apr 24 '16 at 17:12
  • Well, it does though. `prepare` fails because the query is invalid when you don't encapsulate a reserved word field name... – Bitwise Creative Apr 24 '16 at 17:12
  • @RiggsFolly I did not know that. :) He might not be using a version updated with that special case. – Bitwise Creative Apr 24 '16 at 17:14
  • Either way, I like your answer better. – Bitwise Creative Apr 24 '16 at 17:24
  • 1
    `date` is not a reserved word, it's a keyword; two different animals here. Have look for yourself https://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. – Funk Forty Niner Apr 24 '16 at 19:14
  • Sorry, but I failed to see why the answer got more downvotes, or any for that matter. I don't want to rain on the guy's parade, but that wasn't the solution nor does it have anything to do with `date` or any other of the OP's column names. – Funk Forty Niner Apr 24 '16 at 19:16
  • @Fred-ii- I think you mean upvotes, and I agree. I've updated my answer. FAIL. – Bitwise Creative Apr 24 '16 at 20:59
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);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 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. – hayhay Apr 24 '16 at 17:34