PHP Error: "Cannot Pass Parameter 2 by Reference"

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

Cannot pass parameter 2 by reference Error in PHP

You need to use a variable , bind_param takes only variable not values directly.

   $likedone ="";
$sql11->bind_param('ss',$likedone,$Username);

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference (PHP PDO)

According to the PDOStatement::bindParam, the second parameter is the reference to a variable, you are passing a string value, therefore it throws an error.

To fix it, either create a variable to hold that value

$stmt = $conn->prepare("SELECT report_id, task_name, badgeid, report_date, photo_before, photo_after, report_status 
FROM report WHERE badgeid = :badgeid' AND report_status = :report_status;");

$stmt->bindParam(':badgeid', $userid, PDO::PARAM_STR);
$stmt->bindParam(':report_status', $status = "Pending", PDO::PARAM_STR);

or since you always select report with status Pending, don't make it as a parameter

$stmt = $conn->prepare("SELECT report_id, task_name, badgeid, report_date, photo_before, photo_after, report_status 
FROM report WHERE badgeid = :badgeid' AND report_status = 'Pending';");

$stmt->bindParam(':badgeid', $userid, PDO::PARAM_STR);

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();

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference

You have 2 placeholders in your query so you need to bind two variables.

//                       V        and      V
WHERE `user_deped_email`=? OR `user_email`=?

bind_param() should be:

$stmt->bind_param('ss', $depedemail, $personalemail);

$depedemail OR $personalemail is an expression, and an expression is not a variable. You can't pass the result of an expression by reference.

Fatal error: Cannot pass parameter 2 by reference when using PHP and MYSQL

Just for reference. I know this wont help solve your problem, but you could do something like this (see code below) to achieve the same result:

$db = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'dbusername', 'dbpass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO FB_USERS (FULL_NAME,PASSWORD,PASSWORD_SALT,EMAIL_ADDRESS)
VALUES (:FULL_NAME,:PASSWORD,:PASSWORD_SALT,:EMAIL_ADDRESS)";
$stmt = $db->prepare($sql);
$params = array
(
'FULL_NAME'=>'David',
'PASSWORD'=>'sadsad',
'PASSWORD_SALT'=>'adadad',
'EMAIL_ADDRESS'=>'david@gmail.com'
);
$stmt->execute($params)

I find it easier to work with an array and than to just pass it to the statment.
But I guess its just a mather of taste.
Like I said this is just for reference and wont help you resolve your issue.

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

The problem is here, because bind_param() requires all its arguments be passed by reference (except the first arg which is the control string).

$stmt->bind_param("ssssssssssssssss", '1', $adminEmail, $adminPass, $adminUsername, 
'', '', '', '', '1', '1', 'english',);

String literals like '1' are not lvalues, so they cannot be passed by reference.

You can fix it this way:

$one = '1';
$blank = '';
$english = 'english';

$stmt->bind_param("ssssssssssssssss", $one, $adminEmail, $adminPass, $adminUsername,
$blank, $blank, $blank, $blank, $one, $one, $english);

If it were me, I'd use PDO instead of mysqli. PDO has an easier method of passing parameters. No binding step necessary. Just pass as an array to execute(), and this accepts non-lvalues:

$stmt = $pdo->prepare(...);
$stmt->execute([
'1', $adminEmail, $adminPass, $adminUsername, '', '', '', '1', '1', 'english'
]);


Related Topics



Leave a reply



Submit