在我的數據庫類中,我有一個方法query()
,它將SQL語句和參數作爲參數並在數據庫中運行它們。BindParam()無法正常工作
// 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;
}
,這是怎麼了調用該方法
$db->query("SELECT * FROM users WHERE username = :username AND id = :id ", array(':username' => 'sayantan94', ':id' => 1));
但是,當我print_r()
結果集,我得到一個空數組。但是,如果我這樣做
$db->query("SELECT * FROM users WHERE username = :username", array(':username' => 'sayantan94'));
或本
$db->query("SELECT * FROM users WHERE id = :id ", array(':id' => 1));
我得到正確的結果。我的代碼有什麼問題?
This工作。謝謝你的建議。我現在在學習。 –
@SayantanDas請查閱我的文章,以深入解釋您正在使用的一些不良做法,[您的第一個數據庫包裝的兒童疾病](https://phpdelusions.net/pdo/common_mistakes) –
謝謝。我一定會檢查那篇文章。 –