Home  >  Article  >  Backend Development  >  PHP pdo ,为什么不抛出异常?

PHP pdo ,为什么不抛出异常?

WBOY
WBOYOriginal
2016-06-06 20:12:001423browse

如下代码:

<code>/**
     * 写入数据
     *
     * @param  string $table
     * @param  array  $data
     * @return int
     */
    public function insert($table ,$data){
        try {
            $sql = "INSERT INTO ".$this->table($table);
            $columns = $values = '';
            foreach( $data as $k=>$v ) {
                $columns .= "`$k`,";
                $values .= ":$k,";
            }
            $columns = rtrim($columns,',');
            $values = rtrim($values,',');
            $sql = $sql . " ($columns) VALUES ($values)";
            $this->_init($sql ,$data);
            unset($sql ,$columns ,$values);
            return $this->insertId();
        }catch( \PDOException $e ) {
            $this->halt($e->getMessage() ,$this->getSql());
            return false;
        }
    }</code>

会生成如下SQL,

<code>INSERT INTO `dba_tables` (`pid`,`sellerId`,`cardNum`,`processUnit`,`proDate`,`dateOfPro`,`shipDate`,`clinicName`,`doctor`,`created`,`doctorEmail`,`doctorMobile`,`treatTime`,`treatDesc`,`patient`,`patientEmail`,`remark`) VALUES (3,0,'2DFBD1-9E27-CFFD-6846-459CA9E9','第二车间',1460476800,1459008000,1463068800,'某医院','某医生',1460507802,'d@a.com','13333333333','','牙','某某某','c@a.com','asfasdf')</code>

用PHP执行上面的语句,不出现在错误也不抛出异常。
已经开启了pdo的抛出异常模式

<code> $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);</code>

但是用phpmyadmin执行会出现如下错误
PHP pdo ,为什么不抛出异常?

回复内容:

如下代码:

<code>/**
     * 写入数据
     *
     * @param  string $table
     * @param  array  $data
     * @return int
     */
    public function insert($table ,$data){
        try {
            $sql = "INSERT INTO ".$this->table($table);
            $columns = $values = '';
            foreach( $data as $k=>$v ) {
                $columns .= "`$k`,";
                $values .= ":$k,";
            }
            $columns = rtrim($columns,',');
            $values = rtrim($values,',');
            $sql = $sql . " ($columns) VALUES ($values)";
            $this->_init($sql ,$data);
            unset($sql ,$columns ,$values);
            return $this->insertId();
        }catch( \PDOException $e ) {
            $this->halt($e->getMessage() ,$this->getSql());
            return false;
        }
    }</code>

会生成如下SQL,

<code>INSERT INTO `dba_tables` (`pid`,`sellerId`,`cardNum`,`processUnit`,`proDate`,`dateOfPro`,`shipDate`,`clinicName`,`doctor`,`created`,`doctorEmail`,`doctorMobile`,`treatTime`,`treatDesc`,`patient`,`patientEmail`,`remark`) VALUES (3,0,'2DFBD1-9E27-CFFD-6846-459CA9E9','第二车间',1460476800,1459008000,1463068800,'某医院','某医生',1460507802,'d@a.com','13333333333','','牙','某某某','c@a.com','asfasdf')</code>

用PHP执行上面的语句,不出现在错误也不抛出异常。
已经开启了pdo的抛出异常模式

<code> $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);</code>

但是用phpmyadmin执行会出现如下错误
PHP pdo ,为什么不抛出异常?

回答已删除,见谅

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn