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()

有人能给我建议吗?

最佳答案

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();

07-27 21:30