Why I am getting Cannot pass parameter 2 by reference error when I am using bindParam with a constant value?
You need to use bindValue
, not bindParam
bindParam
takes a variable by reference, and doesn't pull in a value at the time of calling bindParam
. I found this in a comment on the PHP docs:
bindValue(':param', null, PDO::PARAM_INT);
P.S. You may be tempted to do this bindValue(':param', null, PDO::PARAM_NULL);
but it did not work for everybody (thank you Will Shaver for reporting.)
Cannot pass parameter 2 by reference error in php PDO
It expects the second paramter to be a variable which can be passed by reference. Assuming $stmt1
is a PDO statement then, as the docs for bindparam say
Unlike PDOStatement::bindValue(), the variable is bound as a reference
and will only be evaluated at the time that PDOStatement::execute() is
called.
Your second param is an expression ($_SESSION['quantity'.$i] * $_SESSION['price'.$i]
) not a variable. Since you appear to want to evaluate the exptression now, I guess you should used bindValue()
instead.
Cannot pass parameter 2 by reference - PDO
The bindParam()
method binds the parameter to a variable. Strings are what are called constants.
In order to make this work you have to pass a variable to the method, like this:
// Prepare the statement
$stmt = $dbh->prepare("INSERT INTO messages (message, sender, key) VALUES (:message, :sender, :key)");
// Bind variables to the parameters
$stmt->bindParam(':message', $message);
$stmt->bindParam(':sender', $sender);
$stmt->bindParam(':key', $key);
// Give the bound variables a value
$message = 'The message...';
$sender = 'Smith';
$key = 'Test-Key';
// And then execute the statement
$stmt->execute();
PHP error: Cannot pass parameter 2 by reference
The error means that the 2nd argument is expected to be a reference to a variable.
Since you are not handing a variable but an integer of value 0, it generates said error.
To circumvent this do:
$a = 0;
$update->bind_param("is", $a, $selectedDate); //LINE 13
In case you want to understand what is happening, as opposed to just fixing your Fatal error
, read this: http://php.net/manual/en/language.references.pass.php
Fatal error: Uncaught Error: Cannot pass parameter 2 by reference PDO
The second argument to bindParam
is passed by reference and therefore cannot be a literal it must be be a variable.
You also have an error in the Query syntax which I fixed, although I dont know the column name to use, please change that before attempting to use this code.
function buscador($DB,$categoria_id,$buscado){
$productos = NULL;
$sql = "SELECT * from producto WHERE categoria_id = ? AND othercol LIKE ?";
$stmt = $DB->prepare($sql);
$p2 = '%' . $buscado . '%';
$stmt->bindParam(1,$categoria_id,PDO::PARAM_STR);
$stmt->bindParam(2,$p2,PDO::PARAM_STR);
$stmt->execute();
$productos = $stmt->fetchAll();
return $productos;
}
Cannot pass parameter by reference in MySQLi
'1' cannot be passed by reference because it's not a variable but a literal. You need to create a variable with mentioned value and bind it instead because bind_param()
function expects variables passed by reference.
Can't pass a constant variable by reference with $stmt->bind_param()?
Quite simply, it's because you can't pass a constant by reference, and there would be no point in doing so, as it is immutable. Constants, by the way, are not variables; it's right there in the name.
When using PDO::bindParam my query returns no results
Try using bindValue instead of bindParam. In the user submitted notes in the PHP manual (php.net/manual/en/pdostatement.bindvalue.php) there is a note about bindParam passing by reference, whereas bindValue doesn't.
PDO integer nullable value issue
You need to use bindValue
like below:-
$Query =$db->prepare("UPDATE TblUsers
SET Age = :age
WHERE
IsActive=1 ")
$Query->bindValue(':age', $a, PDO::PARAM_INT);
$Query->execute();
bindParam
takes a variable, to reference, and doesn't pull in a value at the time of calling bindParam
.
Referecne:-How do I insert NULL values using PDO?
Related Topics
Secure Random Number Generation in PHP
PHP: Replace Umlauts With Closest 7-Bit Ascii Equivalent in an Utf-8 String
PHP, How to Catch a Division by Zero
PHP Multi-Dimensional Array Remove Duplicate
How to Echo a Variable With Single Quotes
How to Extend Access Token Validity Since Offline_Access Deprecation
How to Remove Line Breaks (No Characters!) from the String
How to Get the Base Url With PHP
Submitting a Multidimensional Array Via Post With PHP
PHP: Custom Error Handler - Handling Parse & Fatal Errors
How Unique Is the PHP Session Id
Encrypt and Decrypt Text With Rsa in PHP
File_Get_Contents() Breaks Up Utf-8 Characters
How to Check Uploaded File Type in PHP
Declaration of Methods Should Be Compatible With Parent Methods in PHP