php - fatal error : Call to a member function count() on boolean

标签 php

class db {

private $_pdo ,
        $_query,
        $_error = false,
        $_results ,
        $_count = 0 ; 

private  function __construct () {
    try {

        $host = config::get('mysql/host');
        $database = config::get('mysql/db');
        $username = config::get('mysql/user');
        $pasword = config::get('mysql/password');

        $this->_pdo = new PDO("mysql:host=$host;dbname=$database", $username, $pasword);

        }  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($tabale , $where) {

    return $this->action('DELETE' ,$table , $where) ;
        }  

public function count() {

    return $this->_count ;
}

 public function error() {

    return $this->_error ;
 }
  } 

索引.php

$a = db::getInstance()->get('users',array('username','=','ram')) ;
if(!$a->count()) {

echo "No User" ;
 } else {

echo "OK " ;
  }   

索引文件有错误:

Fatal error: Call to a member function count() on Boolean in line 4.

最佳答案

您的 ->get(..) 方法从 ->action 返回值,它是一个 boolean 所以这样做:

$a = db::getInstance(); // returns the instance
$a->get('users',array('username','=','ram')); // this return true or false
if(!$a->count()) {
    echo "No User" ;
} else {
    echo "OK " ;
}   

此外,您在 ->action() 中还遗漏了一些 $,它必须是:

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

关于php - fatal error : Call to a member function count() on boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33362313/

相关文章:

php - 求和两个复杂的选择

php - 将所有内容传递到一个位置 nginx

php - PHP 中的 SQL 注入(inject),如何将非转义引号发送到服务器?

php - 还有哪些可以轻松集成到 cakephp 的聊天应用程序

php - 图片/文件上传不适用于 materializecss 框架

php - JQuery 弹出窗口.....未捕获类型错误 : Object [object Object] has no method 'dialog'

php - 查询在工作台中有效,但在 PHP 中无效

php - 如何比较自定义格式的两个时间戳?

php - 配置文件 View 更新

javascript - 如何将 jQuery ng-repeat {{x.something}} 传递给 javascript api url,如/api/get_now/{{x.something}}