Home > mysql, php > Multiple inserts with MySQL/PHP

Multiple inserts with MySQL/PHP


You want to insert multiple records in a MySQL table (in a loop, for instance). How to improve the performance?

Solution #1

One way is to regroup all the inserts in one SQL command:

 ('1', 'one'),
 ('2', 'two'),
 ('3', 'three')

Credits go here.

Solution #2

Another way is to regroup the inserts in a transaction:

mysql_query("START TRANSACTION");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("INSERT ...");
mysql_query("COMMIT");   // or "ROLLBACK" if you changed your mind

Credits go here.

Categories: mysql, php Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: