2015-04-20 77 views
1

我有在PHP绑定*功能PDO :: bindValue不确定

我的代码的问题:

$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass); 
$this->db_conn -> query ('SET NAMES utf8'); 
$this->db_conn -> query ('SET CHARACTER_SET utf8_unicode_ci'); 
$this->db_conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->db_conn -> prepare('INSERT INTO `users` (`login`, `password`, `mail`) VALUES(:login,sha1(:password),:mail)'); 
$this->db_conn -> bindValue(':login', $login, PDO::PARAM_STR); 
$this->db_conn -> bindValue(':password', $password, PDO::PARAM_STR); 
$this->db_conn -> bindValue(':mail', $mail, PDO::PARAM_STR); 
$this->db_conn -> execute(); 

错误是:

Fatal error: Call to undefined method PDO::bindParam() 

任何人都可以给我建议?

+1

嗯,你的错误不匹配你的代码,所以要么你不告诉我们的** **实际代码或不是** **真正的错误消息 – Rizier123

+2

[ 'PDO :: prepare()'](http://php.net/manual/en/pdo.prepare.php)返回一个['PDOStatement'](http://php.net/manual/en/class。 pdostatement.php)对象,*那*表示你调用你的绑定和执行函数。 – Sammitch

回答

2

bindParam()bindValue()是由prepare()方法返回的方法PDOStatement。您需要将呼叫的返回值存储到prepare(),并且请拨打bindParam()bindValue()

试试这个:

$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass); 
$this->db_conn -> query ('SET NAMES utf8'); 
$this->db_conn -> query ('SET CHARACTER_SET utf8_unicode_ci'); 
$this->db_conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt = $this->db_conn -> prepare('INSERT INTO `users` (`login`, `password`, `mail`) VALUES(:login,sha1(:password),:mail)'); 
$stmt -> bindValue(':login', $login, PDO::PARAM_STR); 
$stmt -> bindValue(':password', $password, PDO::PARAM_STR); 
$stmt -> bindValue(':mail', $mail, PDO::PARAM_STR); 
$stmt -> execute();