In my DB class I have a method query()
which takes the SQL statement and the parameters as arguments and runs them in the database.
// the parameters are the sql statement and the values, returns the the current object
// the result set object can be accessed by using the results() method
public function query($sql, $params = array()) {
$this->_error = false; // initially the error is set to false
if($this->_query = $this->_pdo->prepare($sql)) {
// if the parameters are set, bind it with the statement
if(count($params)) {
foreach($params as $param => $value) {
$this->_query->bindParam($param, $value);
}
}
// if the query is successfully executed save the resultset object to the $_results property
// and store the total row count in $_count
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
and this is how I am calling the method
$db->query("SELECT * FROM users WHERE username = :username AND id = :id ", array(':username' => 'sayantan94', ':id' => 1));
But when I print_r()
the result set, I am getting an empty array. But if I do this
$db->query("SELECT * FROM users WHERE username = :username", array(':username' => 'sayantan94'));
or this
$db->query("SELECT * FROM users WHERE id = :id ", array(':id' => 1));
I am getting proper result. What is wrong with my code??