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

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

WBOY
Release: 2016-06-06 20:12:00
Original
1461 people have browsed it

如下代码:

<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>
Copy after login
Copy after login

会生成如下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>
Copy after login
Copy after login

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

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

但是用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>
Copy after login
Copy after login

会生成如下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>
Copy after login
Copy after login

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

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

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

回答已删除,见谅

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template