2014-02-09 144 views
1

我已经写了一个代码,并有一个错误,我得到谁能告诉我什么是错的请吗?登录/注册系统获取致命错误php

错误致命错误:调用一个成员函数错误(c)中的非对象上:\ XAMPP \ htdocs中\ loginsistem \的index.php上线6

,但应该说OK!如果IM权

的init.php:

<?php 
session_start(); 

$GLOBALS ['config'] = array(
    'mysql' => array(
     'host' => '127.0.0.1', 
     'username' => 'root', 
     'password' => '', 
     'db' => 'lr' 
    ), 
    'remember' => array(
     'cookie_name' => 'hash', 
     'cookie_expiry' => 604800 
    ), 
    'session' => array(
     'session_name' => 'user' 
    ) 
); 

spl_autoload_register(function($class) { 
    require_once 'classes/' . $class . '.php'; 

}); 

require_once 'functions/sanitize.php'; 
?> 

的index.php:

<?php 
require_once 'core/init.php'; 

$user =DB::getInstance()->get('users', array('username', '=', 'grega')); 

if($user->error()) { 
    echo 'No user'; 
} else { 
    echo 'OK!'; 
} 

?> 

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; 
    } 

} 
+2

你'action'方法并不总是返回'$此'。如果没有,则调用'error()'失败。 –

回答

2

这也可能是在这一部分:

if($this->query($sql, array($value))->error()) { 
    return $this; 
} 

在我看来,你应该修改它:

if(! $this->query($sql, array($value))->error()) { 
    return $this; 
} 
+0

omg你救了我的一天感谢的人,这是问题! – lenart95

0

拆分这样的代码:

// get db instance 
$db = DB::getInstance(); 
// the get function will return $this or false 
$users = $db->get('users', array('username', '=', 'grega')); 

if($users === false || $db->error()) { 
    echo 'No user'; 
} else { 
    echo 'OK!'; 
} 

我也看到在你的代码很奇怪,当你赋值:

$field  = $where[0]; 
$operator = $where[1]; 
$value  = $where[2]; 

如果删除“我认为这将更好地工作。

此外,如果您在代码上进行测试,则不需要在函数中测试错误。

if(in_array($operator, $operators)) { 
    $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; 

    $this->query($sql, array($value); 
    return $this; 
} 
+0

,但它总是返回我没有用户..并且我在数据库中创建了一个用户grega – lenart95

+0

我看到,分配值时也存在问题。 –

+0

但是当我使用这段代码,我得到没有用户每次如果我写在一个有效的用户多数民众赞成在数据库中,或者如果我不没有用户每次 – lenart95