Reputation: 67
I've got a pdo class from the net. Now when I want to use it in the auth class I received any result. Below I put some functions of my pdo.
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
//binds the inputs with the placeholders we put in place
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
//$this->stmt->bindValue($param, $value, $type);
$this->stmt->bindParam($param, $value, $type);
}
//executes the prepared statement
public function execute(){
return $this->stmt->execute();
}
// returns an array of the result set rows
public function resultset(){
$this->stmt->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
// returns a single record from the database
public function resultRow(){
$this->stmt->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
Then here is how I use it in login function of my auth class.
public function login($user, $password){
//$pdo is a new of database class!
$pdo->query("SELECT user,pass FROM users WHERE user = ':user' , pass = ':pass'");
$pdo->bind(':user', $user);
$pdo->bind(':pass', $password);
$result = $pdo->resultRow();
if($result == true) { //do sth
return true;
}
else return false;
}
And it returns false! Since it's the first time that I use pdo in a php project, I'm a bit confused about using it.
What's wrong?
Upvotes: 0
Views: 76
Reputation: 52902
There is no need to use ''
around placeholders. That's the whole point of the placeholder. Remove ''
around :user
and :pass
. The SQL you provide is invalid (in addition to this, you're using ,
instead of AND
to join two parts of your WHERE
statement).
You can also put PDO in a more suitable debug mode by setting PDO::ATTR_ERRMODE
to PDO::ERRMODE_EXCEPTION
. This makes debugging any SQL issues easier.
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
In addition I'd like to comment that having "current" statements inside your PDO class is not a very flexible solution, and will cause leakage of open database handles. This is also true for leaking cursors in the case of the implementation of resultRow
. It might not be an issue for a small web request, but if you try to reuse this code in a more persistent application, you'll run into issues.
It might be better to stick to the standard PDO for now.
Upvotes: 2
Reputation: 586
It's all right with your PDO syntax, so check your $user and $password and connection to DB.
Also, using AND instead coma will be better.
Upvotes: 0