In PHP, when we use MySQL databases, the mysqli extension provides rich functionality to handle database operations. Especially when handling the execution of multiple SQL statements, the mysqli::multi_query method and the mysqli::$errno attribute are very important tools. This article will explain in detail how to use both to effectively handle errors that may occur during multi-statement execution.
mysqli::multi_query :
multi_query is a method in the mysqli class that executes a query containing multiple SQL statements. It can execute multiple statements at once and return the result. Typically, we use it when executing multiple queries, especially when it involves transactions or batch insertions.
$mysqli->multi_query("SQLStatement1; SQLStatement2; SQLStatement3;");
mysqli::$errno :
The errno attribute represents the error code generated by the previous MySQL operation. If an error occurs while executing a SQL query, we can get the error code through $mysqli->errno . Through this property, we can easily check whether an error has occurred and take corresponding measures.
if ($mysqli->errno) {
echo "Error code: " . $mysqli->errno;
}
When using mysqli::multi_query to execute multiple SQL statements, if a statement fails to execute, we can obtain the error information through mysqli::$errno and make corresponding processing. Here is an example to explain how to use them.
<?php
// create MySQLi connect
$mysqli = new mysqli("localhost", "root", "password", "database");
// 检查connect是否成功
if ($mysqli->connect_error) {
die("connect失败: " . $mysqli->connect_error);
}
// Multiple SQL Statement
$sql = "
INSERT INTO users (username, email) VALUES ('user1', 'user1@example.com');
INSERT INTO users (username, email) VALUES ('user2', 'user2@example.com');
INSERT INTO users (username, email) VALUES ('user3', 'user3@example.com');
";
// 执行Multiple SQL Statement
if ($mysqli->multi_query($sql)) {
do {
// Get the result set of the current query
if ($result = $mysqli->store_result()) {
// Process query results
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
// Check if there are more result sets
} while ($mysqli->next_result());
// Check for errors
if ($mysqli->errno) {
echo "Execution error: " . $mysqli->error;
} else {
echo "所有Statement执行成功!";
}
} else {
// 如果多Statement查询失败,Output error message
echo "Execution failed: " . $mysqli->error;
}
$mysqli->close();
?>
Execution of multi_query :
In the code, we use the mysqli::multi_query method to execute multiple SQL statements. It returns true if the execution is successful, otherwise it returns false .
Processing multiple result sets :
After multi_query is executed, we need to check whether there are more result sets through mysqli::next_result , and get the results of the current query through mysqli::store_result . We can process each result set one by one.
Error handling :
After the multi-statement execution is completed, we can check whether an error has occurred through mysqli::$errno . If an error occurs, you can obtain detailed error information through mysqli::error and process it.
Close the connection :
After all operations are completed, remember to close the database connection.
mysqli::multi_query is a powerful method that can be used to execute multiple SQL statements.
Using mysqli::$errno helps us catch and handle errors when executing multi-statements.
When handling multi-statement queries, we must ensure that each result set is processed correctly and check whether any errors have occurred in time.
By using these two together, we can more efficiently capture and handle various possible errors in multi-statement execution, thereby improving the stability and reliability of our code.