Hello,

I came up with the following code to insert a number of rows into a MySQL table in a single query.
The query would look like this:

PHP Code:
$sql "INSERT INTO
 table(id, item) 
VALUES
 (5,item1), (5, item2), (5, item3),  "

As you can see the trailing comma and space should be removed.

Can you please let me know if the following is efficient (is there a better way to remove the last two characters if the string is really long)

PHP Code:

$values
='';

/*****
* $items is an array of values to be inserted
* into individual rows
**/
foreach($items as $item){
$values .= '('.$row['ID'].'),('.$item.'), ';
}

$length strlen($values);
$values substr($values0$length-2);

$sql "INSERT INTO table(id, item) VALUES ".mysql_real_escape_string($values);
$result=$db->query($sql); 
Have you done this before, and if so, did you do it the same way? I would like to know other ways that this is done.

Thanks!