首页 新闻 会员 周边

Fatal error: Call to a member function query() on a non-object in E:\xampp\htdocs\xampp\kyp\MySQLPDO

-1
悬赏园豆:5 [已关闭问题] 关闭于 2016-09-28 20:50

错误经自己排查还是无法解决,求解。。。。。。。。

 

报的错误是:

数据库内容为:

 

 

index.php

<?php
header('Content-Type:text/html; charset = gb2312');
//载入类文件
require('MySQLPDO.class.php');
$dbConfig = array('user' => 'root','pass' => '','dbname' => 'school');
$db = MySQLPDO ::getInstance($dbConfig);
$data = $db -> fetchAll('select * from `student`');
    //输出查询结果
    echo '<pre>';
    print_r($data);
    echo'</pre>';

?>

MySQLPDO.class.php

复制代码
<?php
/*
PDO-MySQL数据库操作类
*/
class MySQLPDO{
    //数据库默认连接信息
    private $dbConfig = array(
    'db' => 'mysql', //数据库类型
    'host' => 'localhost', //服务器地址
    'port' => '3306', //端口
    'user' => 'root',//用户名
    'pass' => '',//密码
    'charset' => 'gb2312',//字符集
    'dbname' => '',//默认数据库
        );
        //单例模式 本类对象引用
        private static $instance;
        //PDO实例
        private $db;
        /*
        私有构造方法
        
        */
        private function _construct($params){
            //初始化属性
            $this -> dbConfig = array_merge($this -> dbConfig,$params);
            //连接服务器
            $this -> connect();
            }
            /*
            获得单例对象
            */
            public static function getInstance($params = array()){
                if(!self :: $instance instanceof self){
                    self :: $instance = new self($params);
                    }
                    return self :: $instance;//返回对象
                }
                /*
                私有克隆
                */
                private function _clone(){}
                /*
                连接目标服务器
                */
                private function connect(){
                    try{
                        //连接信息
                        $dsn = "{$this -> dbConfig['db']}:host = {$this -> dbConfig['host']};
                        port = {$this -> dbConfig['host']};
                        dbname = {$this -> dbConfig['dbname']};
                        charset = {$this -> dbConfig['charset']}";
                        //实例化PDO
                        $this -> db = new PDO($dsn,$this -> dbConfig['user'],$this -> dbConfig['pass']);
                        //设定字符集
                        $this -> db -> query("set names{$this -> dbConfig['charset']}");
                        }catch(PDOException $e){
                            //错误提示
                            die("数据库操作失败:{$e -> getMessage()}");
                            }
                    }
                    /*
                    执行SQL
                    */
                    public function query($sql){
                        $rst = $this -> db -> query($sql);
                        if($rst === false){
                            $error = $this -> db ->errorInfo();
                            die("数据库操作失败:ERROR {$error[1]}({$error[0]}) :({$error[2]})");
                            }
                            return $rst;
                        }
                        /*
                        取得一行结果
                        */
                        public function fetchRow($sql){
                            return $this -> query($sql) -> fetch(PDO :: FETCH_ASSOC);
                            }
                            /*
                            取得所有结果
                            */
                            public function fetchAll($sql){
                                return $this -> query($sql) -> fetchAll(PDO :: FETCH_ASSOC);
                                }
    }
?>
复制代码
kang_ya_ping的主页 kang_ya_ping | 菜鸟二级 | 园豆:243
提问于:2016-07-18 09:24
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册