sudeep
sudeep

Reputation: 170

mysqli: passing Constant in bind_param

How can I pass a constant to the bind_param in mysqli prepared statements. The following works fine by passing variable:

 $stmt->bind_param("i", $id);

But if i try to pass constant, it does not work. For example:

$stmt->bind_param("i", ID);

Gives following error:

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

Thanks

Upvotes: 2

Views: 781

Answers (1)

Damian Yerrick
Damian Yerrick

Reputation: 4664

In MySQLi, the arguments following the type strings are passed by reference, not by value. The only thing you can pass by reference is a variable. The reason for using variables is that you can assign the variables, execute the statement, assign the variables again, execute the statement again, etc. So try this:

$id = ID;
$stmt->bind_param("i", $id);

Or try using PDO's MySQL driver instead of MySQLi. PDO allows you to choose to bind a value instead of a variable to a query parameter. This is useful if you plan to execute a prepared statement only once.

Upvotes: 3

Related Questions