PDO::bindValue 未定义

PDO::bindValue undefined

我在 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() 方法 return 编辑。您需要存储对 prepare() 调用的 return 值,并在其上调用 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();