3

Okay, so I'm trying to update a blog entry, and I'm getting Call to a member function bind_param() on a non-object when I try to run the script. I have done extensive research to see if I could fix it myself, but I must be missing something.

<?php
$stmt = $mysqli->prepare("UPDATE blogentries SET 
  headline = ?, 
   image = ?, 
   caption = ?,  
   article = ?
    WHERE id = ?");
$stmt->bind_param('ssssi',
   $_POST['headline'],
   $_POST['image'],
   $_POST['caption'],
   $_POST['article'],
   $_POST['id']);
$stmt->execute(); 
$stmt->close();

?>

Thanks in advance,

Austen

Update: Here's the db connect

I added the extra $mysqli connection for debugging purposes, and the error occurs even without it.

Austen
  • 283
  • 1
  • 4
  • 16
  • can show full code of that file? where is database connection? –  Jul 21 '13 at 01:25
  • The full code is a few thousand lines long, so I won't do that, but the DB connection is outside of the public_html folder, I'll update main post to show the db connection. – Austen Jul 21 '13 at 01:30
  • how are you calling/including the db connection file that is outside the public_html folder? – Sean Jul 21 '13 at 06:57
  • 1
    Possible duplicate of [Call to a member function bind\_param() on a non-object](http://stackoverflow.com/questions/4488035/call-to-a-member-function-bind-param-on-a-non-object) – miken32 May 12 '16 at 19:19
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Mar 25 '20 at 20:51

3 Answers3

14

$stmt is probably false.

if ($stmt = $mysqli->prepare(...)) {
    $stmt->bind_param(...);
    ...
}
else {
    printf("Errormessage: %s\n", $mysqli->error);
}
bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • 2
    Why would $stmt be false though? – heinkasner Feb 11 '15 at 08:14
  • 1
    It is false when there is a syntax error in your query. In my case I was using a field name with a typo. One way to flush this problem out is to enter the query manually into mysql and compare your query to your PHP code. – mbokil May 29 '15 at 01:59
  • That was very helpful! It told me I had my table name wrong for mysqli->prepare. – Michele Feb 20 '18 at 16:10
1

I solved that by testing the queries manually. It turned out to be a matter of putting each field name between back ticks and removing any quotes against the parameters labeled with question marks inside the query.

Daniel W.
  • 663
  • 1
  • 5
  • 8
-1

If all the connections to the database are correct, try looking for the syntax in the query. For me, I was performing a join and did not specify the "TABLENAME" in the where clause-field for which both the tables have a column.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574