致命错误:在D:\ xampp \ htdocs \ scorpio中的非对象上调用成员函数query()以在第4行执行\ index.php

时间:2014-08-04 12:33:59

标签: php mysql database youtube

我关注了phpacademy.org上的一系列视频,(https://www.youtube.com/watch?v=PaBWDOBFxDc&index=9&list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc

在上面的视频中,关注后,我收到此错误

  

致命错误:在D:\ xampp \ htdocs \ scorpio中的非对象上调用成员函数query()以在第4行执行\ index.php

我的代码是: 的index.php:

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

db::getInstance()->query("SELECT * FROM gebruikers");

?>

init.php:

<?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'db' => 'scorpiodb'
    ),
    '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';
?>

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();
            }
        }

        public function query($sql, $params = array()){
            $this->_error = false;
            if($this->_query = $this->_pdo->prepare($sql)){
                echo 'ok';
            }
        }
    }
?>

在我将第4行包含在index和db中的查询函数之前,一切正常。也许我做的打字错误或我忽略的东西。你能帮帮我吗?

0 个答案:

没有答案