Home > Article > Backend Development > Solution to execute batch mysql statements in PHP_PHP tutorial
When there are multiple mysql statements that need to be executed, such as
$sqls= “insert table a values(1,2); insert table a values(2,3);”
If you need to execute it, there are three methods that can be used in php:
mysql_query
pdo
mysqli
Three methods are available when there is no problem with the sqls statement.
But
Problems will occur when the sql statement is wrong
The first sql error: all three methods return false
The first sql is correct, the second sql is wrong: mysql_query, pdo, mysqli:query also return true. So At this time, you cannot judge whether the statement in your sqls is wrong.
There are several ways to solve this problem:
1 Parse sql statement
Split each sql statement for execution. This solves the problem of executing each statement separately. But this method has several more methods, so it is not advisable.
2 Save the sqls statement as text
Use cmd to execute the command mysql…. .< sqls.sql, and then capture the output. This is also a method, but it feels like it's going around the problem. There should be a better way.
3 Use the mysqli::multi_query method
This method can execute multiple sql statements, then use mysqli::next_result to set the sql offset, and use mysqli::error to get the current offset Error status of moved sql
The following is the sample code for the third method
The code is as follows:
http://www.bkjia.com/PHPjc/326878.html