对成员函数count()的致命错误调用是非对象的

时间:2015-06-27 04:45:05

标签: php mysql pdo

我正在尝试使用pdo查询数据库,但我无法弄清楚问题。我已经为我的数据库详细信息和服务器详细信息创建了一个init文件,并为配置和索引文件以及DB文件创建了配置文件。

的index.php

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

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

  if($user->count()) 
  {
      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;
        }
        public function count()
        {
            return $this->_count;
        }
    }
?>

它报告了一个关于找不到count对象的致命错误:

  

致命错误:调用非对象中的成员函数count()         第6行的C:\ xampp \ htdocs \学生管理系统\ index.php

1 个答案:

答案 0 :(得分:0)

你想声明对象 - 只是从对象类的一部分调用一个实例,只返回一个非完整的对象部分。您正在调用该函数,就好像它只是一个函数而不是它作为整个类的一部分一样,所以然后引用该类的任何其他部分作为一个整体逻辑丢失,因为仅PHP仅将其视为get函数。

要解决:

<?php
  require_once 'core/init.php';
//here
$user = new Db();

$userSelect =  $user->get('users',array('username', '=' , 'raja' ));

...

由此,$user是您的对象。

Sinlgeton

如果您想要将连接创建为单例,则必须使用单例语法引用对类方法(〜函数)的每个引用 - 因此需要将对count()的引用重写为使用::代替->的单例语法。

请阅读http://www.phptherightway.com/pages/Design-Patterns.html