If there is a data table A:
ID Name Title Addtime
If you need to insert an n-piece of data:
$time = time ();
$data = Array (
' name ' => ' name1 ', ' title ' => ' Title1 ', ' addtime ' => $time;
),
Array (
' name ' => ' name2 ', ' title ' => ' Title2 ', ' addtime ' => $time;
),
Array (
' name ' => ' Name3 ', ' Title ' => ' Title3 ', ' addtime ' => $time;
Array (
' name ' => ' Namen ', ' title ' => ' Titlen ', ' addtime ' => $time;
)
;
Before my idea would be, to construct multiple INSERT statements through data, loop the call. Such as:
$sql 1 = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name1 ', ' title1 ', '", $time.) ";
$sql 2 = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name2 ', ' title2 ', '", $time.) ";
......
$sqlN = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' Namen ', ' titlen ', '", $time.) ";
The SQL INSERT statement was later found to be inserted at one time:
$sql = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name1 ', ' title1 ', '", $time. "),";
$sql. = "(' name2 ', ' title2 ', '", $time. "),";
$sql. = "(' Name3 ', ' title3 ', '", $time. "),";
.....
$sql. = "(' Namen ', ' titlen ', '", $time.) ";
By separating multiple data through the "," number, one SQL operation can be used to resolve the insertion of multiple data, which is nearly 9 times times faster than inserting the same data at one time in the case of inserting the data bar number 30. At the same time, because the insert operation is only once, it is similar to a transaction operation, the insertion fails all, and the success of the insert succeeds, making the data management more convenient. So, if you have more than one piece of data that needs to be inserted into the same table, try this way.
When you insert more than one database record earlier, this is often the case:
$b =;
For ($a =0 $a <100; $a + +) {
$sql = INSERT into ' roles ' (' uid ', ' rid ') VALUES (". $a.", ". $b.") ";
mysql_query ($sql);
However, this writing is inefficient and requires multiple execution of SQL statements. If you've used phpMyAdmin to import data, you'll actually find that the above statement can actually be written
INSERT into ' roles ' (' uid ', ' rid ') VALUES
(534,14), (535,14), (536,14), (537,14), (539,14)
So the original code can be rewritten like this.
$b =;
For ($a =0 $a <100; $a + +) {
if ($a ==0)
$sql = INSERT into ' roles ' (' uid ', ' rid ') VALUES (". $a.", ". $b.") ";
else
$sql. = ", (". $a. ",". $b. ")";
}
mysql_query ($sql);