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