php - PDO fetch() returning empty string -
i getting frustrated. have 2 functions have similar "instructions" ie: return values users table in database.
the second 1 works fine, first 1 returning empty value.
here code:
public function validateuser($username, $password) { $stmt = "select password users username = :username limit 1"; if(!($grabuser = $this->db->prepare($stmt))) { return null; } $grabuser->bindparam(":username", $username, pdo::param_str); $grabuser->execute(); $data = $grabuser->fetch(); if(count($grabuser->fetchcolumn()) <= 0) { return null; } echo $data['password'].'s'; if(!password_verify($password,$data['password'])) { return null; } return $this->core->encrypt($data['password']); }
i'm trying display $data['password'] on page test whether returns value database, returning empty, whereas query is returning column because passes
if(count($grabuser->fetchcolumn()) <= 0) { return null; }
condition.
the $username , $password variables both set, no problem.
just in case ask, function does work properly:
public function validatefacebookuser($email, $fid) { $stmt = "select username, password users email_address = :email , connected_fb = '1' , connected_fb_id = :fb limit 1"; if(!($grabuser = $this->db->prepare($stmt))) { return null; } $grabuser->bindparam(":email", $email, pdo::param_str); $grabuser->bindparam(":fb", $fid, pdo::param_int); $grabuser->execute(); $data = $grabuser->fetch(); if(count($grabuser->fetchcolumn()) <= 0) { return null; } return array($data['username'], $this->core->encrypt($data['password'])); }
it turn username , password case. why not work in first function?
thanks.
no, shouldn't mix ->fetchcolumn
, ->fetch
, limit 1
.
what happens ->fetch()
first row. after invocation of ->fetchcolumn()
, there's no more row fetch.
public function validateuser($username, $password) { $stmt = "select password users username = :username limit 1"; if(!($grabuser = $this->db->prepare($stmt))) { return null; } $grabuser->bindparam(":username", $username, pdo::param_str); $grabuser->execute(); $data = $grabuser->fetch(); // fetch once // no need add ->fetchcolumn checking $ret = null; if(!empty($data['password']) && password_verify($password,$data['password'])) { $ret = $this->core->encrypt($data['password']); } return $ret; }
Comments
Post a Comment