PHPpdo, why not throw an exception?

Source: Internet
Author: User
The following code: {code...} generates the following SQL statement, and {code...} executes the preceding statement in PHP. No exception is thrown if an error occurs. If the pdo throw exception mode {code...} is enabled, the following error occurs when you run phpmyadmin.

The following code:

/*** Write data ** @ param string $ table * @ param array $ data * @ return int */public function insert ($ table, $ data) {try {$ SQL = "INSERT ". $ 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 ;}}

The following SQL statement is generated,

Insert into 'dba _ tables '('pid', 'sellerid', 'cardnum', 'processunit', 'prodate', 'dateofpro', 'shipdate', 'clinicname ', 'Doctor', 'created ', 'doctormail', 'doctormobile', 'treattime', 'treatdesc', 'patient', 'patientemail', 'remark') VALUES, '2dfbd1-9e27-cffd-6846-459ca9e9', 'workshop 2', 1460476800,1459008000, 1463068800, 'hospital ', 'Doctor', 1460507802, 'd @ a.com ', '123 ','', 'Ya ', 'xx mou', 'c @ a.com', 'asfasdf ')

If you use PHP to execute the preceding statement, no exception will be thrown even when an error occurs.
The pdo throw exception mode has been enabled.

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

However, the following error occurs when phpmyadmin is used.

Reply content:

The following code:

/*** Write data ** @ param string $ table * @ param array $ data * @ return int */public function insert ($ table, $ data) {try {$ SQL = "INSERT ". $ 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 ;}}

The following SQL statement is generated,

Insert into 'dba _ tables '('pid', 'sellerid', 'cardnum', 'processunit', 'prodate', 'dateofpro', 'shipdate', 'clinicname ', 'Doctor', 'created ', 'doctormail', 'doctormobile', 'treattime', 'treatdesc', 'patient', 'patientemail', 'remark') VALUES, '2dfbd1-9e27-cffd-6846-459ca9e9', 'workshop 2', 1460476800,1459008000, 1463068800, 'hospital ', 'Doctor', 1460507802, 'd @ a.com ', '123 ','', 'Ya ', 'xx mou', 'c @ a.com', 'asfasdf ')

If you use PHP to execute the preceding statement, no exception will be thrown even when an error occurs.
The pdo throw exception mode has been enabled.

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

However, the following error occurs when phpmyadmin is used.

The answer has been deleted. Sorry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.