0

I'm just having a strange prepare statement failed which I donno why

function send_notification($notification_member_ID,$notification_activity_type,$notification_parent_ID,$notification_parent_type){
  global $mysqli;
  $stmt = $mysqli->prepare("INSERT INTO notifications (`notification_member_ID`, `notification_activity_type`, `notification_parent_ID`, `notification_parent_type`) VALUES ( ? , ? , ? , ? )");
  $stmt->bind_param('iiii', $notification_member_ID, $notification_activity_type, $notification_parent_ID, $notification_parent_type);
  if($stmt->execute()){
    return 1;
  }else{
    error_log("!send_notification(mysqli,$notification_member_ID,$notification_activity_type,$notification_parent_ID,$notification_parent_type) at ".date("Y-m-d H:i:s",time())."\n", 3, "{$dir_error_log}/transaction.log");
  }
}

this is my debug attempt which failed

$product_ID=2;
$stmt = $mysqli->prepare("SELECT product_holder_ID FROM market where product_ID=? ");
$stmt->bind_param('i', $product_ID);
$stmt->execute();
$stmt->bind_result($product_holder_ID);
while($stmt->fetch()){
    $mysql->send_notification($product_holder_ID,'1',$product_ID,'1');
}

Fatal error: Call to a member function bind_param() on a non-object

while I succeed if I move it out of the loop

$product_ID=2;
$stmt = $mysqli->prepare("SELECT product_holder_ID FROM market where product_ID=? ");
$stmt->bind_param('i', $product_ID);
$stmt->execute();
$stmt->bind_result($product_holder_ID);
while($stmt->fetch()){
}
$mysql->send_notification($product_holder_ID,'1',$product_ID,'1');

whats the black box inside that make it goes error in the first attempt?


More information:

$mysqli- error : Commands out of sync; you can't run this command now

Anonymous
  • 1,405
  • 4
  • 17
  • 26
  • 1
    Why do you log a lot of useless data but not very $mysqli->error which actually explains what's wrong with prepare? – Your Common Sense Aug 08 '13 at 12:06
  • I think that you should pass `global $mysqli;` into the function so you are sure it is available to your function. Might even resolve your issue. I don't know. There are 2 `bind_param`s. Which one throws the error? – AmazingDreams Aug 08 '13 at 12:11
  • its the bind param inside the function throw error – Anonymous Aug 08 '13 at 12:13
  • http://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now – AmazingDreams Aug 08 '13 at 12:13
  • and I have added my $mysqli->error output – Anonymous Aug 08 '13 at 12:13
  • @Anonymous - that error message means that you're trying to re-use the same database handle that's already being used. You can create a second database connection and use that for your INSERT; or you can refactor the code so that your SELECT statement can be closed before you start the INSERTs. – andrewsi Aug 08 '13 at 12:47
  • Does this answer your question? [Commands out of sync; you can't run this command now](https://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) – Dharman Feb 09 '20 at 18:48

0 Answers0