Unable to pass parameter 2 by reference - uuid PDO

I am trying to insert UUID() along with my INSERT request.

 $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 request returns 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 here?

+6
source share
3 answers

The second argument to bindParam is passed by reference and must be a variable. You directly pass values ​​that are not allowed.

Put the UUID() directly in the query, because if it is connected as a parameter, it will be placed in the query as a quoted string and will not be evaluated by the UUID value.

You can put 1 directly in the request. Or assign variable 1 variable and specify this variable as the second argument when binding the parameter :type_id .

 $type_id = 1; $stmt->bindParam(':type_id', $type_id, PDO::PARAM_INT); 
+20
source

There is no need to bind it in this case, just include it in your query:

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

.. then bind :type_id , as you already know.

+2
source

Your INSERT INTO users (users_uuid, type_id) VALUES (SELECT UUID (), 1)

not a valid mysql query

try getting uuid () first and then paste this value into the users table

0
source

All Articles