Jump to content

[SOLVED] Which insert method is better


HuggieBear

Recommended Posts

I have about 150 rows that I'm inserting into my database.

 

Is it better to loop through the data doing a mysql_query() for each insert, or loop through the data creating a sql statement with a single insert.  See below for example:

 

<?php
// Multiple inserts in a loop
foreach ($row as $k => $v){
   $sql = "INSERT INTO table_name VALUES ($row[$k], $row[$k])";
   mysql_query($sql, $db);
}
?>

 

or

 

<?php
// Single insert after creating a string in the loop
foreach ($row as $k => $v){
   $sql .= "($row[$k], $row[$k]),";
}

// Remove final comma
rtrim($sql, ",");

$final_sql = "INSERT INTO table_name VALUES " . $sql;
mysql_query($sql, $db);
?>

 

Regards

Rich

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.