Multiple MySQL INSERT Statements in a Single Query Using PHP
In PHP, it is possible to execute multiple INSERT statements in a single query using the following syntax:
$string1 = "INSERT INTO table1 (column1, column2) VALUES (value1, value2);\n";
$string1 .= "INSERT INTO table2 (column1, column2) VALUES (value3, value4);\n";
$string1 .= "INSERT INTO table3 (column1, column2) VALUES (value5, value6);";
mysql_query($string1) or die(mysql_error());
However, this approach is generally not recommended for several reasons:
Optimized Approach
A better approach to inserting multiple rows is to use multiple INSERT statements with the following syntax:
$sql = "INSERT INTO table1 (column1, column2) VALUES ";
for ($i = 0; $i By using this approach, each row is inserted separately, providing better data integrity and transaction support. Additionally, the database can optimize each statement individually, potentially improving efficiency.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3