致命错误:设置PDO连接时未定义的类常量'FETCH_ASSOC'

时间:2013-10-09 06:23:47

标签: php mysql pdo

这是我的PDO连接类:

class DB {

private static $objInstance;

private static $dsn;
private static $user;
private static $pwd;


/*
 * Class Constructor - Create a new database connection if one doesn't exist
* Set to private so no-one can create a new instance via ' = new DB();'
*/
private function __construct() {}

/*
 * Like the constructor, we make __clone private so nobody can clone the instance
*/
private function __clone() {}

private function getConfig()
{
    $config = new Config();
    $db = $config->getConfig('dbcxn');
    DB::$dsn = $db['dsn'];
    DB::$user = $db['user'];
    DB::$pwd = $db['password'];
}

/*
 * Returns DB instance or create initial connection
* @param
* @return $objInstance;
*/
public static function getInstance(  ) {

    if(!self::$objInstance){
        self::getConfig();
        self::$objInstance = new \PDO(DB::$dsn, DB::$user, DB::$pwd);
        self::$objInstance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        self::$objInstance->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    }

    return self::$objInstance;

} # end method

/*
* Passes on any static calls to this class onto the singleton PDO instance
* @param $chrMethod, $arrArguments
* @return $mix
*/
final public static function __callStatic( $chrMethod, $arrArguments ) {

$objInstance = self::getInstance();

    return call_user_func_array(array($objInstance, $chrMethod), $arrArguments);

} # end method
}

我从http://php.net/manual/en/book.pdo.php采取了上述内容,对我自己进行了一些修改但是当我调用该类时,我收到了这个错误:

Fatal error: Undefined class constant 'FETCH_ASSOC'...

有谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

因为你没有Config()类......

其中:

$config = new Config();

你需要先上课!

编辑: 你需要定义Config类!

像:

class Config
{
 // construct here
}