d-evil has asked for the wisdom of the Perl Monks concerning the following question:
I have a problem in my code which is having two DBI mysql queries in a loop. I have two tables master_table and active_table and it goes like this
Master_tableactive_tableRouter_Name Serial_No Fru_Desc Location Status RouterA AA FRUA NULL Online RouterA BB FRUB FPC0 PIC0 Online RouterA CC FRUC FPC1 PIC1 Online RouterA DD FRUD FPC2 PIC2 Online RouterA EE FRUE FPC3 PIC3 Online RouterB FF FRUF FPC5 PIC5 Online RouterB GG FRUG FPC6 PIC6 Online RouterB HH FRUH FPC7 PIC7 Online RouterB II FRUI FPC8 PIC8 Online RouterB JJ FRUJ FPC9 PIC9 Online
I have the code like this:RouterA CC FRUC FPC1 PIC1 Online RouterA DD FRUD FPC2 PIC2 Online RouterA EE FRUE FPC3 PIC3 Online RouterB AA FRUA FPC0 PIC1 Online RouterB FF FRUF FPC5 PIC5 Online RouterB GG FRUG FPC6 PIC6 Online RouterB HH FRUH FPC7 PIC7 Online RouterB II FRUI FPC8 PIC8 Online RouterB JJ FRUJ FPC10 PIC10 Online
In the above code i am printing the rows for both tables by router name wise but instead i am getting the following result. (the following output is formatted for reading purpose, irrespective of the printf in the code)my $sth_m = $dbh->prepare("Select Serial_No,Fru_Desc,Status f +rom master_table2 where Router_Name = ?") or die "Cannot prepare mast +er_table for selecting rows based on router name:" .$dbh->errstr; my $sth_a = $dbh->prepare("Select Serial_No,Fru_Desc from act +ive_table2 where Router_Name = ?") or die "Cannot prepare active_tabl +e selecting rows based on router name" .$dbh->errstr; foreach my $rtr(@routers) { $sth_m->execute($rtr); $sth_a->execute($rtr); while (my @row_master_db = $sth_m->fetchrow_array) + { push(@db_rows,[@row_master_db]); } foreach $test1(@db_rows) { print @$test1; } print "\n"; while (my @row_active_db = $sth_a->fetchrow_array) { push(@live_rows,[@row_active_db]); } foreach $test2(@live_rows) { print @$test2; } print "\n"; ....... (here i have the code to compare each row from @db_rows to each an +d every row of @live_rows ....... }
In the second iteration of the loop(for RouterB), results of both the queries are returned for the first printf as well as the second printf Actually i need to get the results of first execute ($sth_m->execute) for the first printf and $sth_a->execute for the second printf.First Iteration - Master_table AA FRUA Online BB FRUB Online CC FRUC Online DD FRUD Online EE FRUE Online First Iteration - active_table EE FRUE DD FRUD CC FRUC Second iteration - Master_table AA FRUA BB FRUB CC FRUC DD FRUD EE FRUE FF FRUF Online GG FRUG Online HH FRUH Online II FRUI Online JJ FRUJ Online Second iteration - active_table EE FRUE DD FRUD CC FRUC AA FRUA FF FRUF GG FRUG HH FRUH II FRUI JJ FRUJ
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with two mysql queries in a loop
by lamp (Chaplain) on Sep 19, 2008 at 04:03 UTC | |
by d-evil (Novice) on Sep 19, 2008 at 05:22 UTC |