我在php中的bind *函数有问题
我的代码是:
$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()
有人可以给我建议吗?
答案 0 :(得分:4)
bindParam()
和bindValue()
是PDOStatement
方法返回的prepare()
方法。您需要将调用的返回值存储到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();