将参数绑定到PDO语句时出现问题

时间:2011-12-08 20:05:44

标签: php mysql pdo

我有一个奇怪的问题,我只是没有找到解决方案。问题是准备好的sql语句没有绑定值,参数甚至传递它们通过execute函数。相反,它会插入':blah'占位符。正如我所说,我已经尝试过bindParam,bindValue和这个方法都没有结果。但是,我现在再试一次。

我输出了在执行调用之前发送的参数。

Array ( [:username] => schenn [:salt] => NW5552wekj5155cNr52O54q56 [:hashpass] => 5e54240aec6294873d11d6ac3e5b135136a1b671 [:email] => monkey@monkey.com [:state] => OR [:country] => USA [:last_login] => 12/08/2011 ) 

以下是代码:

$query = "INSERT INTO player_acct (username, salt, hashpass, email, state, country, last_login)
                VALUES (':username', ':salt', ':hashpass', ':email', ':state', ':country', ':last_login')";
$stmt = $pdoI->prepare($query);
$params = array(":username" => $this->username, ":salt" => $this->salt, ":hashpass" => $this->hashpass,
                        ":email" => $this->email, ":state" => $this->state, ":country" => $this->country, ":last_login" => $this->last_login );
$stmt->execute($params);

2 个答案:

答案 0 :(得分:4)

你不应该引用SQL中的占位符。请尝试以下SQL字符串:

$query = "INSERT INTO player_acct (username, salt, hashpass, email, state, country,  
          last_login) VALUES (:username, :salt, :hashpass, :email, :state, :country, :last_login)";

答案 1 :(得分:0)

绑定变量时,不引用SQL语句中的绑定值。

$query = "INSERT INTO player_acct (username, salt, hashpass, email, state, country, last_login) VALUES (:username, :salt, :hashpass, :email, :state, :country, :last_login)";

还要确保$this->email等设置正确。