0

我有以下连接代码:

$this->connectionString = 'mysql:host=' . db_config_DBConfig::$MySQLserverName . ';dbname=' . db_config_DBConfig::$MySQLdbName . ';port=' . db_config_DBConfig::$MySQLport .';connect_timeout=15';
    $this->connection = new PDO($this->connectionString, db_config_DBConfig::$MySQLuserName, db_config_DBConfig::$MySQLpassCode);
    $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

这样做之后:

 $this->connection->prepare("CALL CreateRecord('testingRecord')")

它给了我以下错误:

Fatal error: Call to a member function prepare() on a non-object in 

当我使用 var_dump 打印对象时,它给了我这个:

object(db_mysql_DBConnect)#1 (3) { ["connectionString":"db_mysql_DBConnect":private]=> string(70) "mysql:host=localhost;dbname=testingrecord;port=3306;connect_timeout=15" ["connection"]=> object(PDO)#2 (0) { } ["assocData":"db_mysql_DBConnect":private]=> array(0) { } } 

我不确定我在这里缺少什么。任何帮助将不胜感激...

4

1 回答 1

0

我猜 PDO 构造函数由于错误的连接凭据而失败。如果连接失败,直接传递ERRMODE_EXCEPTION给构造函数,使连接触发异常:

$this->connection = new PDO(
    $this->connectionString,
    db_config_DBConfig::$MySQLuserName,
    db_config_DBConfig::$MySQLpassCode,
    array (
        PDO::ATTR_ERRMODE =>  PDO::ERRMODE_EXCEPTION
    )
);
于 2013-05-01T21:41:47.867 回答