【发布时间】:2014-06-05 20:29:01
【问题描述】:
我不知道为什么它会返回Call to a member function fetchAll() on a non-object 错误,因为我在 _sth 对象上使用了 fetchAll 方法。
有人知道为什么会这样吗?
FormValidator PHP
$users = $this->_db->query("SELECT * FROM $ruleValue WHERE $input = ?", array($inputValue));
if($users->count()) {
$this->addError($input, $rule);
}
方法
public function query($sql, $params = array()) {
$this->_sth = $this->_dbh->prepare($sql);
if($this->_sth = $this->_sth->execute($params)) {
return $this->_sth->fetchAll(PDO::FETCH_OBJ);
}
return false;
}
【问题讨论】:
-
我在函数上使用它的唯一原因是我不必一遍又一遍地重复相同的代码。
-
execute()返回一个布尔值。它没有fetchAll()函数。只需if($this->_sth->execute($params)) {。 -
没有太多可重复的代码,但安全性存在巨大漏洞。您应该删除查询中的所有变量
-
WHERE $input这里例如