错误:PDO :: __ construct()期望参数2为字符串

时间:2015-10-25 05:42:35

标签: php mysql pdo

db.php

class db {
    private static $_instance = null;
    private $_pdo, 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'));
            echo 'hello ';
        } catch (PDOException $e) {
            die($e - > getMessage());
        }
    }
    public static
    function getInstance() {
        if (!isset(self::$_instance)) {
            self::$_instance = new db();
        }
        return self::$_instance;
    }
}

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

     } 
  }  

initialize.php

   session_start() ;

     $GLOBALS['config'] = array(
    'mysql' => array (
    'host' => '127.0.0.1',
    'user' => 'root' ,
    'password' => '',
    'db' => 'login' 
    ) ) ;

添加了所有三个文件 错误

  显示主db :: getInstance()中的

错误:错误:PDO :: __ construct()期望参数2为字符串。

添加提交的更多详细信息。我在其中添加了哪些行提交问题。

1 个答案:

答案 0 :(得分:0)

而不是

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

尝试

    <?php
$host = config::get('mysql/host');
$database = config::get('mysql/db');
$username = config::get('mysql/user');
$pasword = config::get('mysql/password');

$dbh = new PDO('mysql:host=$host;dbname=$database', $username, $pasword);
?>