php 无法通过引用传递参数 2 - uuid PDO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13408388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot pass parameter 2 by reference - uuid PDO
提问by fishcracker
I am trying to insert UUID()together with my INSERTquery.
我试图UUID()与我的INSERT查询一起插入。
$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);
此查询返回此错误无法通过引用传递参数 2 ... 在第 51 行。它指向这条线$stmt->bindParam(':uuid',"SELECT UUID()",PDO::PARAM_STR);
What am I doing wrong in here?
我在这里做错了什么?
回答by air4x
The second argument to bindParamis passed by reference and should be a variable. You are directly passing the values which is not allowed.
to的第二个参数bindParam是通过引用传递的,应该是一个变量。您正在直接传递不允许的值。
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.
将UUID()直接在查询,因为如果它被绑定为参数,将它放置在查询中引用字符串并不会被计算为一个UUID值。
You can place the 1directly in the query too. Or assign 1to a variable and give that variable as the second argument while binding the parameter :type_id.
您也可以将1直接放在查询中。或者分配1给一个变量并在绑定参数时将该变量作为第二个参数:type_id。
$type_id = 1;
$stmt->bindParam(':type_id', $type_id, PDO::PARAM_INT);
回答by Madbreaks
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_idas you already are.
..然后:type_id像你一样绑定。
回答by user1283182
your INSERT INTO users (users_uuid, type_id) VALUES (SELECT UUID(), 1)
你的 INSERT INTO 用户 (users_uuid, type_id) VALUES (SELECT UUID(), 1)
is not a valid mysql query
不是有效的 mysql 查询
try to get uuid() first, then insert the value into your users table
尝试先获取 uuid(),然后将该值插入到您的用户表中
回答by Frank Forte
If you are passing a value, use this:
如果要传递值,请使用以下命令:
$type_id = 1;
$stmt->bindValue(':type_id', $type_id, PDO::PARAM_INT);
If you are binding a parameter (where you pass a reference to the variable), then do this:
如果要绑定参数(在其中传递对变量的引用),请执行以下操作:
$stmt->bindParam(':type_id', $type_id, PDO::PARAM_INT);
// now you can update $type_id, since it is a reference, and execute multiple times:
foreach($id as $i){
$type_id = $i;
$stmt->execute();
}

