CodeIgniter在使用PDO作为驱动程序时没有捕获数据库错误

I'm using CodeIgniter 2.1.3, and I'm using the active record class with PDO as my database driver.

I came across an issue where CodeIgniter isn't reporting errors with incorrect update statements. I can write a query which clearly violates duplicate keys or has incorrect column names, but CodeIgniter gives no error whatsoever with:

echo $this->db->_error_message(); //returns nothing
echo $this->db->_error_number();  //returns '0000'

However, as soon as I change over to using MySQL as the driver, I receive error messages.

I've even tried $this->db->query(.... //violating statement to bypass the active record class with no luck and more errors.

I have $db['default']['db_debug'] = TRUE;.

Am I out of luck using PDO, or are there some additional config settings that need to be changed when using PDO with CodeIgniter?

If you look up the PDO driver, you can see that the PDO connection is done with PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT which does not throw out any errors.

In general you want PDO to throw exceptions, in which case you pass the below with the connection options.

array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

If you want to use PDO, use PDO direct. PDO is a good abstraction layer. If you are going to be using the CI DB Driver anyway, I don't see any real advantages of selecting this over mysql or vice-versa, because you will be writing your queries the CI way regardless.