Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对成员函数count()的调用为非对象时发生致命错误_Php_Mysql_Pdo - Fatal编程技术网

Php 对成员函数count()的调用为非对象时发生致命错误

Php 对成员函数count()的调用为非对象时发生致命错误,php,mysql,pdo,Php,Mysql,Pdo,我正在尝试使用pdo查询数据库,但我无法解决问题。我已经为我的数据库详细信息和服务器详细信息创建了一个init文件,为配置和索引文件以及数据库文件创建了配置文件 index.php <?php require_once 'core/init.php'; $user = Db::getInstance()->get('users',array('username', '=' , 'raja' )); if($user->count()) { e

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

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


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

Db.php

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

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

  if($user->count()) 
  {
      echo "No user";
  }
  else{
    echo "OK!";
  }


?>
<?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() 第6行的C:\xampp\htdocs\Student Management system\index.php


您想要声明对象——只需从对象类的一部分调用一个实例,就会返回一个不完整的对象部分。调用函数时,如果它只是一个函数,而不是整个类的一部分,那么引用整个类的任何其他部分时,逻辑就会丢失,因为PHP仅将其视为
get
函数

要解决:

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

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

...

是否在index.php中包含Db.php文件?错误消息非常清楚,
$user
是非对象。调试$user并查看它是否是一个对象,它实际上定义了
count
方法。我会说,一开始就提出了这个问题failed@SuryaJothika,您好,这不是Java在不指定其文件的情况下自动包含类。如果未包含类,则会出现
类未找到
致命错误如果试图创建单例类,请尝试创建
\u构造()
public并使其将
$this
返回到一个静态变量,如
if(!isset(self:$singleton))self:$singleton=$this;返回自我::$singleton类似的东西。然后你可以做
$con=newdb()
,在包含PDO连接的
\uU构造中有一个
受保护的函数connect()
。只要我的两分钱。