数据库调用在子文件夹

时间:2016-10-18 14:01:47

标签: php mysql pdo frameworks require

我在子文件夹中调用框架类时遇到了问题。我有一个init文件,包括db调用和路由到我的不同类文件夹。然后我添加了一个新的init文件,允许我在子文件夹中使用我的框架,例如这个圈出来的文件:

enter image description here

一切似乎都路由正常,但现在我的数据库类无法正常工作。我收到以下错误:

注意:数组转换为字符串

警告:PDO :: __ construct()期望参数2为字符串,数组在

中给出

对于此代码:

$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

问题是,它适用于我在root中的普通文件:ie:index,register等。它只适用于新的init文件调用。这是我的两个init文件路由:

原创

require_once 'core/init.php';

初始文件

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

    require_once 'functions/sanitize.php';

新的调用和初始化文件:

require_once __DIR__ . '/../core/init_account.php';

新的初始文件

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

    require_once '../functions/sanitize.php';

有没有人知道为什么以下代码适用于我的原始init文件,而不是我的新文件?

$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

其余的初始文件:

$GLOBALS['config'] = array(
    'mysqli' => array(
        'host' => 'localhost',
        'username' => 'actual',
        'password' => 'actual',
        'db' => 'actual'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expiry' => 604800
    ),
    'session' => array(
        'session_name' => 'user',
        'token_name' => 'token'
        )
    );

编辑:配置类:

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

0 个答案:

没有答案