我在类构造函数中创建新的 PDO 连接,然后仅在该类中使用它。如果发生一些错误,我会得到致命错误而不是PDOException
.
致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[42S22]: Column not fou.....
我的代码是:
class Helper
{
private $_db;
function __construct($config = FALSE)
{
$this->_db = new PDO($config['database']['dsn'], $config['database']['username'], $config['database']['password'], $config['database']['options']);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
$config = array(
'database' => array(
'dsn' => 'mysql:host='.$dbhost.';dbname='.$dbname,
'username' => $dblogin,
'password' => $dbpass,
'options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
)
);
try {
$helper = new Helper($config);
} catch (PDOException $error) {
die('Database Error: ' . $error->getMessage());
}
那么有可能吗?