php - PDOStatement::bindParam && PDOStatement::bindValue not working -
i've been reading lot of posts here in stackoverflow , documentation info @ php dot net.
here's code i'm tryng:
example 1
$id = 1; $sth = $this->pdo->prepare('select * users_table id_usr = ?'); $sth->execute(array(intval($id)));
example 2
$id = 1; $sth = $this->pdo->prepare('select * users_table id_usr = :id'); $sth->bindparam(':id', $id, pdo::param_int); $sth->execute();
example 3
$id = 1; $sth = $this->pdo->prepare('select * users_table id_usr = :id'); $sth->bindvalue(':id', intval($id)); $sth->execute();
if try this:
$sth = $this->pdo->prepare('select * users_table id_usr = 1'); $sth->execute();
i result i'm expecting it's not solution i'm looking for.
hope can me, in advance.
////////// edit 1
at end of of examples i'm doing this:
$arr = $sth->errorinfo(); print_r($arr);
the return is:
array ( [0] => 00000 [1] => [2] => )
/////////// edit 2
here's code
class user { private $registry; private $userid; private $userlan; private $fullname; private $dob; private $email; private $sex; private $nationality; private $valid; // user valid? private $pdo; // pdo reference /** * constructor del usuario. se puede construir de dos formas, pasando email y password o pasando una id de usuario. * @param registry $registry. * @param int $id * @param string $email * @param string $password * @param string $username * @return void */ public function __construct ( registry $registry, $id, $email, $password, $username) { echo "constructor user id = $id "; $this->valid = false; $this->registry = $registry; if($id = 0 && $username != '' && $password != '') { // ... $this->valid = true; } //else if($id > 0) else { // $id = intval($id); echo "second if"; $this->pdo = $registry->getobject('db')->getpdo(); try { $sth = $this->pdo->prepare('select * users_table id_usr = :id'); $sth->execute(array(':id' => intval($id))); //$sth->execute(array(intval($id))); $arr = $sth->errorinfo(); print_r($arr); $result = $sth->fetch(pdo::fetch_both); print_r($result); } catch(pdoexception $e) { echo $e->getmessage(); } echo "passing query"; if($sth->rowcount() ==1) echo "yeeeha"; else echo "not yeeha (".$sth->rowcount().") "; // ... $this->valid = true; } }
your error checking not correct:
$sth = $this->pdo->prepare('select * users_table id_usr = :id'); $sth->bindparam(':id', $id, pdo::param_int); $sth->execute(); $arr = $sth->errorinfo();
this assumes pdo::prepare()
returns object, that's not documentation says:
return values
if database server prepares statement, pdo::prepare() returns pdostatement object. if database server cannot prepare statement, pdo::prepare() returns false or emits pdoexception (depending on error handling).
i suggest make life easier , configure pdo throw exceptions. example taken from manual:
$dbh = new pdo($dsn, $user, $password); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); ^^^^^^^^^^^^^^^^^^^^^^
Comments
Post a Comment