PDO的bindParam与bindValue的区别

ARGUS

[来源于Stackoverflow: What is the difference between bindParam and bindValue?]
PDOStatement::bindParam 与 PDOStatement::bindValue()不同, 变量被以引用方式绑定到点位符上而且仅仅当调用PDOStatement::execute()时才会去计算具体被绑定变量在PDOStatement::execute()被调用时的值.

So, for example:

php<?php
$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(); // 将执行 WHERE sex = 'female'

or

php<?php
$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(); // 将执行 WHERE sex = 'male'
阅读 4k

千里码博客
A.R.G.U.S.网络安全小组
1.6k 声望
152 粉丝
0 条评论
你知道吗?

1.6k 声望
152 粉丝
宣传栏