我得到一个"调用成员函数count()"错误。我已经尝试了各种方法来解决这个问题,但现在我被卡住了。
问题在于:
var_dump($data, $field);
if($user->count()) {
$this->_data = $data->first();
这是我的代码:
class user {
private $_db,
$_data;
public function __construct($user = null) {
$this->_db = DB::getInstance();
}
public function create($fields = array()) {
if(!$this->_db->insert('users', $fields)) {
throw new exception('There was a problem creating an account.');
}
}
public function find($user = null) {
if($user) {
$field = (is_numeric($user)) ? 'id' : 'username';
$data = $this->_db->get('users', array($field, ' = ', $user));
var_dump($data, $field);
if($user->count()) {
$this->_data = $data->first();
return true;
}
}
return false;
}
public function login($username = null, $password = null) {
$user = $this->find($username);
if($user) {
if($this->data()->password === hash::make($password, $this->data()->$salt)) {
echo 'OK!';
}
}
return false;
}
private function data() {
return $this->_data;
}
}
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db').'', ''.Config::get('mysql/username').'', ''.Config::get('mysql/password').'');
echo 'Connected';
} catch(PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
private function action($action, $table, $where = array()) {
if(count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field) {
$values .= '?';
if($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO users (`". implode('`, `', $keys) . "`) VALUES ({$values})";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function update($table, $id, $fields) {
$set = '';
$x = 1;
foreach($fields as $name => $value) {
$set .= "{$name} = ?";
if($x < count($fields)) {
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE id ={$id}";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function results() {
return $this->_results;
}
public function first() {
return $this->_results[0];
}
public function error() {
return $this->_error;
}
public function count() {
return $this->_count;
}
}
请帮忙!
答案 0 :(得分:0)
修改强> 您在调用$ this-&gt; _db-&gt; get时使用了错误的运算符。你传入&#39; =&#39;但可接受的运营商是&#39; =&#39; (空间不足)。结合以下建议可以解决您的问题。
看起来您需要$data->count()
而不是$user->count()
public function find($user = null) {
if($user) {
$field = (is_numeric($user)) ? 'id' : 'username';
$data = $this->_db->get('users', array($field, '=', $user));
if($data && $data->count() > 0) {
$this->_data = $data->first();
return true;
}
}
return false;
}
你应该使用更好的变量名,你可以在实际值不是用户对象的几个地方使用$ user。
public function find($user = null) {
这是一个数字或字符串,而不是尝试
public function find($queryParam = null) {
$user = $this->find($username);
这是一个布尔值,而不是尝试
$userFound = $this->find($username)'
答案 1 :(得分:0)
您的query
方法会使用上次执行的查询计数更新属性$_count
。您有一个公共方法来访问此属性; count()
。您的数据库类对象保存在属性_db
中。因此,我们可以做到以下几点;
if($this->_db->count()) {
$this->_data = $data->first();
答案 2 :(得分:-2)
如果这段代码:
$field = (is_numeric($user)) ? 'id' : 'username';
表示$user
有时可能是某个对象以外的东西,为什么你无条件地做
if($user->count()) {
哪会要求$user
始终是一个对象?