主要问题不断出现在代码中

时间:2013-11-29 08:14:44

标签: php database performance object

我在浏览数据库时遇到问题 - 我收到此错误:

致命错误:在第5行的C:\ xampp \ htdocs \ ooplr \ index.php中的非对象上调用成员函数error() < / p>

<?

php
require_once "core/init.php";
$users = DB::getInstance()->get('users',array('username','=','alex'));

if($users->error()){ ***<--- ERROR MESSAGE ON THIS LINE!!!***
    echo  'No user';
} else {
    echo 'OK!';
}

错误消息在第5行。如何将$用户转变为对象?

get方法工作正常 - 测试它!

那么问题似乎是什么问题?

我刚做了一个var_dump($ users),它正在打印bool(false)......为什么会这样?

1 个答案:

答案 0 :(得分:0)

您修改过的数据库类

添加了一堆方法,一旦找到罪魁祸首

就要删除
<?php
class DB{
  const OP_EQUALS  = 0;
  const OP_M_THAN  = 1;
  const OP_L_THAN  = 2;
  const OP_ME_THAN = 3;
  const OP_LE_THAN = 4;
  /**
   * Singleton pattern
   * @var PDO object
   */
  private static $instance = null;
  /**
   * @var PDO object
   */
  private $pdo;
  /**
   * prepared statements
   * @var PDOStatement object
   */
  private $query;
  /**
   * This class is incomplete, this is important but neglected, time to change that...
   * @var PDOStatement object
   */
  private $results;
  /**
   * Holds the result of calling stmt->rowCount() for the current query
   * @var integer the behaviour of this is not guaranteed
   */
  private $count = 0;
  private function __construct(){
    try {
      $host = Config::get('mysql/host');
      $data = Config::get('mysql/db');
      $user = Config::get('mysql/username');
      $pass = Config::get('mysql/password');
      $dsn = 'mysql:host='.$host.';dbname='.$data;
      $this->pdo = new PDO($dsn,$user,$pass);
      $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
      // not for production
      die($e->getMessage());
    }
  }
  public static function getInstance(){
    if (!isset(self::$instance)) self::$instance = new DB;
    return self::$instance;
  }
  /**
   * This is a blanket fetch all, seems ok
   * @param string $q
   * @param array $params
   * @return DB
   */
  public function query($q, array $params=array()) {
    $this->count = null;
    $this->query = $this->pdo->prepare($q);
    $this->query->execute($params);
    $this->results = $this->query->fetchAll(PDO::FETCH_CLASS,'stdClass');
    $this->count = $this->query->rowCount();
    return $this;
  }
  /**
   * This is a cute function, but I do not recomend it's use
   * @param string $action
   * @param string $table
   * @param array $where
   * @return DB
   */
  private function action($action, $table, array $where=array()){
    static $operators = array('=','>','<','>=','<=');
    if (count($where)===3) {
      list($op1,$operator,$op2) = $where;
      if (in_array($operator, $operators)) {
        $q = $action.' FROM '.$table.' WHERE '.$op1.' '.$operator.' ?';
        return $this->query($q,$operators);
      }
    }
    return null;
  }
  /**
   * This abstraction seems convenient but it will become a nightmare later on
   * @param string $table
   * @param array $where
   * @return DB
   */
  public function get($table, array $where=array(1,0,1)){
    return $this->action('SELECT *',$table,$where);
  }
  /**
   * This abstraction seems convenient but it will become a nightmare later on
   * @param string $table
   * @param array $where
   * @return DB
   */
  public function delete($table, array $where=array(1,0,1)){
    return $this->action('DELETE',$table,$where);
  }
  public function getResultSet(){
    return $this->results;
  }
  /*
   * start debugging functions
   */

  /**
   * Returns a prepared statement
   * @param string $q
   * @return PDOStatement
   */
  public function debug_statement($q) {
    return $this->pdo->prepare($q);
  }
  /**
   * Directly executes a statement
   * @param string $q
   * @param int $fetch_options
   * @return PDOStatement
   */
  public function debug_query($q, $fetch_options=PDO::FETCH_ASSOC) {
    return $this->pdo->query($q,$fetch_options);
  }
  /**
   * Get metadata from table
   * @param string $table
   * @return string
   */
  public function describe_table($table) {
    $stmt = $this->debug_query('DESC '.$table);
    return '<pre>'.print_r($stmt->fetchAll(),1).'</pre>';
  }
  /**
   * Get contents from table
   * @param string $table
   * @return string
   */
  public function dump_table($table) {
    $stmt = $this->debug_query('SELECT * FROM '.$table);
    return '<pre>'.print_r($stmt->fetchAll(),1).'</pre>';
  }
}

一个新的调试脚本

我希望这不会空洞,否则我会非常难过

// debugging information
// let's use a blanket statement to get all rows in the database
$db = DB::getInstance();
echo 'Currently debugging!'.PHP_EOL;
echo 'Get a clear look at our `Users` table: '.$db->describe_table('users');
echo 'Dump all rows: '.$db->dump_table('users');

使用此DB类,然后运行脚本。给我们一些反馈意见

相关问题