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?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With