can't catch PDOException when my query has error, why?

I'm using CodeIgniter and I have the following code to fetch all records from database:

try {
     $sth = $this->db->conn_id->prepare($query);  
     $sth->bindParam(':something', $something, PDO::PARAM_INT);
     $sth->execute();  
     //...
    }
     catch (PDOException $e) {
            //error goes here
    } 

I made a mistake on $query which I put LIMIT right before ORDER BY. ie:

SELECT `something` from some_table LIMIT 0, 10 ORDER BY some_other_field DESC (bad)

instead of

SELECT `something` from some_table ORDER BY some_other_field DESC LIMIT 0, 10 (correct)

but when I run my code, I didn't catch any PDOException (just return no result), even I set PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

so why is that? so how to catch the query error?

Update 1 : In CodeIgniter pdo_driver.php file, I have already changed the db_connect() and db_pconnect():

function db_connect()
    {
        $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
        $this->options['PDO::ATTR_EMULATE_PREPARES'] = FALSE;

        return new PDO($this->hostname, $this->username, $this->password, $this->options);
    }

    // --------------------------------------------------------------------

    /**
     * Persistent database connection
     *
     * @access  private called by the base class
     * @return  resource
     */
    function db_pconnect()
    {
        $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
        $this->options['PDO::ATTR_PERSISTENT'] = TRUE;

        return new PDO($this->hostname, $this->username, $this->password, $this->options);
    }

However I won't catch any PDOException, unless I added: $this->db->conn_id->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); before I run the execute(). Why?

链接地址: http://www.djcxy.com/p/69570.html

上一篇: Mysql PDO执行始终返回TRUE

下一篇: 当我的查询有错误时无法捕获PDOException,为什么?