Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to update mysql within a perl 'for each' loop ?

Status
Not open for further replies.

rab54

Programmer
Joined
Jan 28, 2004
Messages
112
Location
GB
Hi gurus -

Here is my problem - I am reading in a file,line by line -
I then need to update mysql depending on the 'id' -

eg

foreach $i (@indata) {
chomp($i);

get the fields

#### if I update mysql here, it updates on each iteration - therefore only leaving the last record ###

}

### if I do the update here I only get the first record !

Any help gratefully appreciated !

cheers all

Rab

 
I don't understand what your problem is. If you want to update your MySQL database for each record you want to do it inside the foreach loop.
 
Cheers for the reply Rosenk !

What happens is that the database is updated on loop of the foreach -

eg data -> smith jones bloggs

for each { #(first loop= smith,2nd loop=jones,3rd=bloggs)

updates db with smith,then jones then bloggs

}

what I need is it to be smith for the first record selected,then jones then bloggs etc ..

code sample -


open (NEWINF,$update_file) || die "Can't open eclipse_file ! : $!";

@indata = <NEWINF>;

foreach $i (@indata) {
chomp($i);

($name_1, etc)

$stmt = &quot;UPDATE table SET name_1 = '$name_1' WHERE id = '8564' &quot;;

}

any help to you ?

cheers




 
Not exactly. What do you want the database to look like after the process is done?
 
rab,

Can you give us a look at the format of your data;

is it something like
8654, Jones, 8655, Smith, 8733, Alas

while (<>) {
($name[0], $id[0],$name[1], $id[1], $name[2],$id[2])=split(/,/,$_);
$i=0;
foreach (@id} {
$stmt = &quot;UPDATE table SET name_1 = $name[$i] WHERE id = $id[$i] &quot;;
$i++;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top