1
我得到使用ZF2並且將對DBAdapter問題。如何獲得異常執行select語句ZF2
我正在做一個選擇語句,並試圖發生一些錯誤時,得到TE例外。
我有我的select語句塊內try ... catch ...但問題是當我設置一個「錯誤的查詢」,異常沒有拋出。
這裏我的服務:
return array(
'db' => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=my_dbname;host=localhost',
'username' => 'root',
'password' => 'root',
'driver_options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
],
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
);
這裏我的查詢:
try {
$db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$sql = "SELECT * FROM `table`";
$statement = $db->query($sql);
$res = $statement->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}