无法使用PDO连接到数据库

时间:2015-04-07 07:00:29

标签: php pdo

我是PHP编程的新手,尝试使用PDO连接到数据库。但是在收到错误的同时:

  

" Class' DB'在第5行和第34行的D:\ xampp \ htdocs \ web \ oop \ index.php中找不到;

请帮忙。"给出了细节。提前谢谢。

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

芯/的init.php

     require_once 'functions/sanitize.php';

     $GLOBALS['config'] = array(
    'mysql' => array(
    'host'=> '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'db' => 'oop'
    ),
'remember' => array(
    'cookie_name' => 'hash',
    'cookie_expiry' => 604800
    ),
'session' => array(
    'session_name' => 'user'
    )
  );


    spl_autoload_register(function($class)
   {

require_once 'classes/' . $class . '.php';
 }
    )


 ?>

类/ db.php中

     <?php
    namespace application\libs;
    use POD;
    class DB
    {

        private static $_instance = null;
        private $_pdo,
                $_query,
                $_error = false,
                $_results,
                $_count = 0;


        private function __construct()
        {
            try{
                $this->_pdo = new POD('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;
        }


    }



    ?>

的index.php

    <?php
       require_once 'core/init.php';
       DB::getInstance();
    ?>

1 个答案:

答案 0 :(得分:0)

似乎您自动加载课程的路径有误,请尝试以下操作:

spl_autoload_register(function($class)
   {

require_once '/../classes/' . $class . '.php';
 }
    )

或者请在需要文件时使用绝对路径,这将始终确保您的文件将加载。

spl_autoload_register(function($class)
   {

require_once __DIR__ .'/classes/' . $class . '.php';
 }
    )