Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot pass parameter 2 by reference - uuid PDO

Tags:

php

mysql

pdo

I am trying to insert UUID() together with my INSERT query.

$handle->beginTransaction();
// Define query
$query = "INSERT INTO users (users_uuid, type_id) VALUES (:uuid, :type_id)";
// Prepare statement
$stmt = $handle->prepare($query);
// Bind parameters
$stmt->bindParam(':uuid',"SELECT UUID()",PDO::PARAM_STR);
$stmt->bindParam(':type_id',1,PDO::PARAM_INT);
// Execute query
$stmt->execute();
$handle->commit();

This query return this error Cannot pass parameter 2 by reference ... on line 51. And it points to the line $stmt->bindParam(':uuid',"SELECT UUID()",PDO::PARAM_STR);

What am I doing wrong in here?

like image 549
fishcracker Avatar asked Nov 16 '12 00:11

fishcracker


2 Answers

The second argument to bindParam is passed by reference and should be a variable. You are directly passing the values which is not allowed.

Place UUID() directly in the query because if it is bound as a parameter, it would be placed in the query as a quoted string and will not be evaluated to a UUID value.

You can place the 1 directly in the query too. Or assign 1 to a variable and give that variable as the second argument while binding the parameter :type_id.

$type_id = 1;
$stmt->bindParam(':type_id', $type_id, PDO::PARAM_INT);
like image 151
air4x Avatar answered Nov 08 '22 03:11

air4x


There's no need to bind it in this case, simply include it in your query:

$query = "INSERT INTO users (users_uuid, type_id) VALUES (UUID(), :type_id)";

..then bind :type_id as you already are.

like image 35
Madbreaks Avatar answered Nov 08 '22 05:11

Madbreaks