Hi,

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_table
Router_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
active_table
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
I have the code like this:
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 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)
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
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.

In reply to Problem with two mysql queries in a loop by d-evil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.