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
bindParamis 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
1directly in the query too. Or assign1to a variable and give that variable as the second argument while binding the parameter:type_id.