Operation error or failure, but no error when doing database operation

Source: Internet
Author: User
Tags php and mysql

The first thing to note is:

PHP code running errors and the interaction between PHP and MySQL, the error in MySQL, is not necessarily synchronous.

Case 1: Using PDO, no settings pdo::attr_errmode

When the PDO is not set to error handling mode, the default isPDO::ERRMODE_SILENT,即--不报错,不管是使用PDO::query还是PDO::exec都不报错,但是可以通过PDO::errorCode()来判断是否出错,或者query和exec的返回值来判断sql语句是否操作成功,注意此时PHP的代码是成功执行了的,注意区分。

If you want to explicitly prompt for an error, you can set the error handling mode to pdo::errmode_exception.

Correction Method:

Pdo::setattribute (Pdo::attr_errmode, pdo::errmode_exception)

  

Case 2: Using mysqli, use Mysqli::query to perform update, delete, drop, and insert commands for these DML types

When using Mysqli::query to execute the DML command, it is actually executed, and the result is returned, but the operation failed, not only the error, but also continue to execute the following code, which is very important to note.

Mysqli::query executes the DML command, returns True if successful, and returns False if it fails. So if you want to use Mysqli::query to perform DML, be sure to add judgment, depending on the return value to decide whether to do the next step, do not because of a failed database operation, resulting in your subsequent work of the bug, and this bug is hidden, not easy to find.

  

Operation error or failure, but no error when doing database operation

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.