dqwcdqs358367 2015-10-25 05:42
浏览 141
已采纳

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

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' 
    ) ) ;

all three files added Error:

in main db::getInstance() call error shown Error: PDO::__construct() expects parameter 2 to be string .

add more details for submitting. what lines i add to in it submit question .

  • 写回答

1条回答 默认 最新

  • dongyinzhi4689 2015-10-25 05:48
    关注

    Instead of below

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

    Try

        <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度