By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

How to insert 2 arrays into 2 fields in MySQL using Perl

100+
P: 170
hi guys,

i dunno if this post should be in Perl or MySQL

but i'm using Perl DBI to do manupilations now in MySQL

i've got problem trying to input 2 arrays of data into 2 fields at the same time

say for example now i got
Expand|Select|Wrap|Line Numbers
  1.  
  2. @array1 = [ 1 ,2 , 3];
  3. @array2 = [4, 5, 6];
  4.  
and i got say 2 fields User and Time

i tried using this code
Expand|Select|Wrap|Line Numbers
  1. foreach $user(@array1){
  2. $sth -> $dbh ->perpare("INSERT INTO `trial` (User) VALUES ('$user')");
  3. $sth->execute();
  4.  
  5. foreach $time(@array2){
  6. $sth -> $dbh ->perpare("INSERT INTO `trial` (Time) VALUES ('$time')");
  7. $sth->execute();
  8.  
i got the results like:
Expand|Select|Wrap|Line Numbers
  1. User    Time
  2. 1          NULL
  3. 2          NULL
  4. 3          NULL
  5. NULL    4
  6. NULL    5
  7. NULL    6
  8.  
how should i change or input my code so i can get this in the database
Expand|Select|Wrap|Line Numbers
  1. User      Time
  2. 1           4
  3. 2           5
  4. 3           6
  5.  
May 14 '08 #1
Share this Question
Share on Google+
3 Replies


ronverdonk
Expert 2.5K+
P: 4,258
This is a coding problem, i.e. a Perl question and I do not think we in the MySQL forum can write the Perl code for that. So this thread is moved to the Perl forum.

moderator
May 14 '08 #2

100+
P: 170
alright thanks
once again perl guys
i need your help once again
:)
May 15 '08 #3

Atli
Expert 5K+
P: 5,058
I'm not a Perl person, but MySQL can insert into multiple columns and rows in one query:
For example:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO userTable(UserID, Time) VALUES
  2. (1, 4), (2, 5), (3, 6);
  3.  
Try re-writing your Perl code to take advantage of that.
May 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.