MySQLi prepared insert statement fails

2019-04-18 03:19发布

问题:

I am updating my PHP to use mysqli:: instead of mysql_* and I have run into an issue with INSERT statements. I have the following statement:

$stmt = $link->prepare("INSERT INTO `table` (`a`, `b`, `c`) VALUES(?, ?, ?)");
$stmt->bind_param("sss", $a, $b, "0");
$stmt->execute();

I have checked $stmt and it is a proper mysqli_stmt object. It is prepared properly, but for some reason, the statement won't execute. I just get a 500 error from my server.

What am I missing?

Edit

I've determined that the issue is coming from the bind_param method.

Edit 2

Okay, so the error PHP is giving me is this:

Fatal error: Cannot pass parameter 4 by reference in...

This points to the line of bind_param

回答1:

You can't pass a constant to bind_param. Put the value in a variable first:

$status = "0";
$stmt->bind_param("sss", $oid, $cid, $status);
$stmt->execute();


标签: php sql mysqli