douxing6532 2015-04-07 07:00
浏览 50
已采纳

无法使用PDO连接到数据库

I am new in PHP programming trying to connect to database using PDO. But while doing getting error as:

"Class 'DB' not found in D:\xampp\htdocs\web\oop\index.php on line 5"

please help." details are given blow. thanks in advance.

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

core/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';
 }
    )


 ?>

classes/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条回答 默认 最新

  • douxiong3245 2015-04-07 07:15
    关注

    It seems like your paths for autoloading your classes is wrong, please try the following:

    spl_autoload_register(function($class)
       {
    
    require_once '/../classes/' . $class . '.php';
     }
        )
    

    or please use an absolute path when requiring files, this will always ensure that your files will load.

    spl_autoload_register(function($class)
       {
    
    require_once __DIR__ .'/classes/' . $class . '.php';
     }
        )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致