I have a mysqli function that is inserting a row into a table. I’m getting back the error
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of
fields in prepared statement
This is odd to me since I thought insert nto returns true or false. Here is the function that is using the sql statement.
function add_project_deadline ($mysqli, $project_id, $deadline) {
if ($stmt = $mysqli->prepare("INSERT INTO `project_deadline` (project_id, deadline)
VALUES (?, ?)")){
$stmt->bind_param('is', $project_id, $deadline);
$stmt->execute();
$stmt->bind_result($return);
$stmt->close();
return $return;
} else {return false;}
}
My question is why am I getting this bind_result error and how do I fix it.
Since you are executing an
INSERTquery which doesn’t have any results, you don’t need or usebind_resultfor fetching data.bind_resultis for binding to columns used in a SELECT statement.mysqli_stmt::execute()returnstrueon success orfalseon failure so simply assign the value of that to your$returnvariable.If
execute()returns false, theINSERTstatement failed for some reason. If it returnstrue, theINSERTwas successful and you can determine the number of rows inserted by looking at themysqli_stmt::$affected_rowsproperty of your statement object.That’s why you’re getting that error, hope it helps. You can find most of this information on the manual page for
mysqli_stmt::execute().