为什么这个有效
class xyz{
private $_db;
function __construct(){
//database connection
}
function abc($login,$pass,$email){
$l = "login";
$check = $this->_db->prepare("SELECT userid FROM users WHERE login = ?");
$check->execute(array($login));
$res1 = $check->fetch(PDO::FETCH_NUM);
return var_dump($res1);
}
}
如果我将登录的行选择更改为变量,则代码返回bool false
class xyz{
private $_db;
function __construct(){
//database connection
}
function abc($login,$pass,$email){
$l = "login";
$check = $this->_db->prepare("SELECT userid FROM users WHERE ? = ?");
$check->execute(array($l,$login));<<<<<-----THIS $l FAILS TO WORK
$res1 = $check->fetch(PDO::FETCH_NUM);
return var_dump($res1);
}
}
使用不同的行选择及其值进行3个完全相同的查询的最佳方法是什么?
答案 0 :(得分:2)
您无法在预准备语句中对列名进行参数化:http://us3.php.net/manual/en/book.pdo.php#69304
此处还有更多信息:Which tokens can be parameterized in PDO prepared statements?
答案 1 :(得分:1)