php bindParam 和 bindValue 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1179874/
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
What is the difference between bindParam and bindValue?
提问by koen
What is the difference between PDOStatement::bindParam()and PDOStatement::bindValue()?
采纳答案by acrosman
The answer is in the documentation for bindParam:
答案在以下文档中bindParam:
Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.
与 PDOStatement::bindValue() 不同,该变量被绑定为一个引用,并且只会在 PDOStatement::execute() 被调用时被评估。
And execute
和 execute
call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
调用 PDOStatement::bindParam() 将 PHP 变量绑定到参数标记:绑定变量将它们的值作为输入传递并接收其关联参数标记的输出值(如果有)
Example:
例子:
$value = 'foo';
$s = $dbh->prepare('SELECT name FROM bar WHERE baz = :baz');
$s->bindParam(':baz', $value); // use bindParam to bind the variable
$value = 'foobarbaz';
$s->execute(); // executed with WHERE baz = 'foobarbaz'
or
或者
$value = 'foo';
$s = $dbh->prepare('SELECT name FROM bar WHERE baz = :baz');
$s->bindValue(':baz', $value); // use bindValue to bind the variable's value
$value = 'foobarbaz';
$s->execute(); // executed with WHERE baz = 'foo'
回答by lonesomeday
From the manual entry for PDOStatement::bindParam:
从手册条目中PDOStatement::bindParam:
[With
bindParam] UnlikePDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time thatPDOStatement::execute()is called.
[With
bindParam] 与 不同PDOStatement::bindValue(),变量被绑定为一个引用,并且只会在PDOStatement::execute()被调用的时候被评估。
So, for example:
因此,例如:
$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindParam(':sex', $sex); // use bindParam to bind the variable
$sex = 'female';
$s->execute(); // executed with WHERE sex = 'female'
or
或者
$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindValue(':sex', $sex); // use bindValue to bind the variable's value
$sex = 'female';
$s->execute(); // executed with WHERE sex = 'male'
回答by Pascal MARTIN
Here are some I can think about :
以下是我能想到的一些:
- With
bindParam, you can only pass variables ; not values - with
bindValue, you can pass both (values, obviously, and variables) bindParamworks only with variables because it allows parameters to be given as input/output, by "reference" (and a value is not a valid "reference" in PHP): it is useful with drivers that (quoting the manual) :
- 使用
bindParam,您只能传递变量;不是价值观 - 有
bindValue,你可以通过这两个(值,很明显,和变量) bindParam仅适用于变量,因为它允许通过“引用” (并且值在 PHP 中不是有效的“引用”)作为输入/输出给出参数:它对驱动程序很有用(引用手册):
support the invocation of stored procedures that return data as output parameters, and some also as input/output parameters that both send in data and are updated to receive it.
支持调用存储过程,将数据作为输出参数返回,有些还作为输入/输出参数,既发送数据又更新接收数据。
With some DB engines, stored procedures can have parameters that can be used for both input (giving a value from PHP to the procedure) and ouput (returning a value from the stored proc to PHP) ; to bind those parameters, you've got to use bindParam, and not bindValue.
对于某些数据库引擎,存储过程可以具有可用于输入(从 PHP 向过程提供值)和输出(从存储过程向 PHP 返回值)的参数;要绑定这些参数,您必须使用 bindParam,而不是 bindValue。
回答by Nezar Fadle
From Prepared statements and stored procedures
Use bindParamto insert multiple rows with one time binding:
用于bindParam一次性插入多行:
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
回答by Denilson Sá Maia
For the most common purpose, you should use bindValue.
对于最常见的目的,您应该使用bindValue.
bindParamhas two tricky or unexpected behaviors:
bindParam有两个棘手或意外的行为:
bindParam(':foo', 4, PDO::PARAM_INT)does not work, as it requires passing a variable (as reference).bindParam(':foo', $value, PDO::PARAM_INT)will change$valueto string after runningexecute(). This, of course, can lead to subtle bugs that might be difficult to catch.
bindParam(':foo', 4, PDO::PARAM_INT)不起作用,因为它需要传递一个变量(作为参考)。bindParam(':foo', $value, PDO::PARAM_INT)$value运行后会变成字符串execute()。当然,这会导致可能难以捕捉的细微错误。
Source: http://php.net/manual/en/pdostatement.bindparam.php#94711
来源:http: //php.net/manual/en/pdostatement.bindparam.php#94711
回答by tfont
The simplest way to put this into perspective for memorization by behavior (in terms of PHP):
将其置于行为记忆的最简单方法(就 PHP 而言):
bindParam:referencebindValue:variable
bindParam:参考bindValue:多变的
回答by Thielicious
You don't have to struggle any longer, when there exists a way lilke this:
当存在这样的方法时,您不必再挣扎了:
$stmt = $pdo->prepare("SELECT * FROM someTable WHERE col = :val");
$stmt->execute([":val" => $bind]);

