致命错误:调用成员函数prepare()吗? [英] Fatal error: Call to a member function prepare()?

查看:100
本文介绍了致命错误:调用成员函数prepare()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying query the database using PDO , but i am struggling in a fatal error any one help what actually happened...

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服务器运行它时,它显示错误,这是对非对象的致命错误……它是在编写方法中的实际错误.

when i run this using xampp server it showing error ,which is an fatal error on a non object...the actual error it in prepare method.

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

推荐答案

在非对象上调用成员函数prepare()..."表示$this->_pdo->prepare($sql))中的对象"_pdo"不存在

"Call to a member function prepare() on a non-object..." means that the object "_pdo" in $this->_pdo->prepare($sql)) doesn't exist.

反过来,这意味着从未调用过构造函数(应初始化_pdo).

This, in turn, implies that the constructor (which should initialize _pdo) was never called.

因为应将其命名为 __construct()(而不是"__constructs" ).

Because it should be named __construct() (not "__constructs").

此外,正如Fred -ii-所指出的, bindValue()拼写错误.

Also, as Fred -ii- already pointed out, bindValue() is mis-spelled.

而且,出于完整性考虑:

And, for completeness:

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

这篇关于致命错误:调用成员函数prepare()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆