2014-10-19 128 views
-1

所以我得到这个致命错误:“致命错误:调用/Applications/XAMPP/xamppfiles/htdocs/website/index.php中的非对象的成员函数count()第6行“。为了以防万一,我会只给你一大部分代码。调用非对象的成员函数count()。从一个.php到另一个

这是我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!'; 
} 

我在做什么错?

+0

'$ user'不是一个对象,通过'var_dump($ user);'检查它是什么。因为它不是一个对象,所以不能通过' - > count()'调用它的方法 – Cheery 2014-10-19 22:51:12

回答

0

您的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将抛出一个异常。

0

error message告诉你$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条款。

相关问题