Reputation: 75
I have problem with bind* functions in php
my code is :
$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();
error is:
Fatal error: Call to undefined method PDO::bindParam()
Can anybody give me advice?
Upvotes: 1
Views: 4696
Reputation:
bindParam()
and bindValue()
are methods of PDOStatement
which is returned by the prepare()
method. You need to store the return value of the call to prepare()
and call bindParam()
or bindValue()
on that.
Try this:
$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();
Upvotes: 5