Page 1 of 1

INSERT data in more than one row at the same time, HELP!

Posted: Sat May 24, 2003 2:51 pm
by Hernan
Hi, i just started with mysql and i'm trying to insert the following variables into the database, if any one could give an example of how to insert data in more than one row at hte same time, i think that what i'm missing here.
Thx in advanced for your reply.



This file is saved as db.php
I GOT THIS ERROR
Parse error: parse error, unexpected T_ELSE in /Users/osx/Sites/mysql/db.php on line 6

Code: Select all

<?php
$username = "*****";
$password = "*****";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");
$selected = mysql_select_db("DB_Name",$dbh) or die ("Could not select DB_Name");
$data = array( array('$friend_last_name1', '$friend_name1', '$friend_email1', '$ip_address', '$date'),
				 array('$friend_last_name2', '$friend_name2', '$friend_email2', '$ip_address', '$date'),
				 array('$friend_last_name3', '$friend_name3', '$friend_email3', '$ip_address', '$date'));
$sth = $dbh->prepare("INSERT INTO table_name VALUES(Last_name,First_name,Email_address, ip_address, Date)");
foreach ($data as $row) {
    $dbh->execute($sth, $row);
{
  print "successfully inserted record";
}
else {
          print "Failed to insert record";
}
mysql_close($dbh);
?>

I got it to work after reading this Article here.

Posted: Sat May 24, 2003 6:22 pm
by Hernan
http://www.mysql.com/doc/en/Insert_speed.html

I hope this help some one... :P

here is a multi value example above. This one contains 3 rows to be inserterd.

mysql> INSERT INTO a VALUES (1,23),(2,34),(4,33);

The multivalue statement is not clear.

I assume the values correspond to the columns in the order they appear in the table?

What if the table has 9 columns but we only need to update 2 columns, and those 2 columns are in the 3rd and 7th positions?

What if we need to change the 3rd & 5th columns for one row, and the 6th & 9th columns for the another row?

Explicit usage, syntax and limitations would be helpful.


insert into table table_name (col5, col7) values (x1,y1), (x2,y2), (x3,y3)


WORKS!!