对非对象的成员函数count()的调用.从一个.php到另一个php


Call to a member function count() on a non-object. From one .php to another

所以我收到了这个致命错误:"致命错误:在/Applications/XAMPP/examplefiles/htdocs/website/index.php的第6行调用非对象的成员函数count()"。我只会给你很大一部分去我的代码以防万一。

这是我的DB.php

<?php
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'));      
    } 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;
}
public 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 error() {
    return $this->_error;
}
public function count() {
    return $this->_count;
}
}

这是我的index.php

<?php
require_once 'core/init.php';
$user = DB::getInstance()->get('users', array('username', '=', 'alex'));
if(!$user->count()) {
    echo 'No user';
} else {
   echo 'OK!';
}

我做错了什么?

您的get()方法中出现错误,将导致以下sql语句:

SELECT* * FROM ....

您可以从get方法中删除*,或者从action方法中删除它,并在get方法中添加一个空格。

您可能需要后一个选项才能使delete方法工作:

public function get($table, $where) {
    return $this->action('SELECT *', $table, $where);
}

和:

...
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
...

除此之外,您假设PDO会抛出异常,但您需要首先告诉PDO。

您可以在构造函数中添加该选项:

$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),
                      Config::get('mysql/username'),
                      Config::get('mysql/password'),
                      $options);

现在,当出现问题时,PDO将抛出一个异常。

错误消息告诉您$user是非对象。查看action()函数的代码,我猜测$user被设置为false,因为导致了SQL语法错误

public function get($table, $where) {
  return $this->action('SELECT*', $table, $where); // there is an extra asterisk here
}

$sql = "{$action} * FROM {$table} WHERE {$field} {$operator} ?";

请注意SELECT子句中的额外星号。