致命错误:调用成员函数prepare()?

时间:2015-06-27 02:22:22

标签: php mysql pdo phpmyadmin

我正在尝试使用PDO查询数据库,但我在致命的错误中挣扎 任何人帮助实际发生的事情......

CONFIG.PHP

<?php


class Config{
    public static function get($path = null){
        if($path){
             $config = $GLOBALS['config'];
             $path = explode('/',$path);


             foreach ($path as $bit) {
                 if (isset($config[$bit])) {
                        $config = $config[$bit];
                     }
                 }
                 return $config;
        }
        return false;
    }
 }

的init.php

<?php
    session_start();

    $GLOBALS['config'] = array(
        'mysql' => array(
            'host' => '127.0.0.1',
            'username' => 'root',
            'password' => 'rajaraman',
            'db' => 'sms'
        ),
    '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()->query("SELECT username FROM users WHERE username = ?",array('raja')); 
  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 __constructs()
        {
            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();
                # code...
            }
            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->fetchAll(PDO::FETCH_OBJ);
                        $this->_count = $this->_query->rowCount();
                 }  
                 else 
                 {
                    $this->error=true;
                 }
            }
            return $this;               
        }
        public function error(){
            return $this->error;
        }
    }
?>

当我使用xampp服务器运行它时显示错误,这是非对象的致命错误...它在准备方法中的实际错误。

致命错误:在第35行的C:\ xampp \ htdocs \ Student Management system \ classes \ Db.php中的非对象上调用成员函数prepare()

1 个答案:

答案 0 :(得分:1)

&#34;在非对象上调用成员函数prepare()...&#34;意味着对象&#34; _pdo&#34;在$this->_pdo->prepare($sql))中并不存在。

这反过来暗示了构造函数(应该初始化_pdo)从未被调用过。

因为它应该命名为__construct()(不是&#34; __构造&#34;)。

另外,正如Fred -ii-已经指出的那样,bindValue()拼写错误。

而且,为了完整性:

$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);