Has any one else encountered problems when nesting mySQL select statements using the DBI module?

I know full well that it's possible to do something like this...(because I have and I use it)

my $sql = qq{ SELECT * FROM slname}; $qry = $dbhutils->prepare($sql); $qry->execute(); my $slnameHTML = ""; while (@row = $qry->fetchrow_array){ $slname = $row[0]; $slID = $row[1]; $slnameHTML = $slnameHTML . "<option value=\"$slID\">$slname<\ +/option>\n"; } $qry->finish();

However if I do a similier kind of while loop but this time I use one of the pieces of data to perform another SELECT query, the DBI module whinges at me with the following error..

DBD::mysql::st fetchrow_array failed: fetch() without execute() at....

Here's the code and it appears to be quite straight forward...?

my $sql = qq{ SELECT * FROM titles ORDER BY slID, countryID, languageI +D, title}; $qry = $dbh->prepare($sql); $qry->execute(); while(@row = $qry->fetchrow_array){ print $cgi->h2("Hello!!!"); my $countryID = $row[0]; my $languageID = $row[1]; my $slID = $row[2]; my $title = $row[3]; my $countryname = &getCountryNameFromID($countryID); print $cgi->p("$slID"); print $cgi->p("$countryname"); print $cgi->p("$languageID"); print $cgi->p("$title"); print $cgi->p(""); } $qry->finish();

The "&getCountryNameFromID" performs a simple SELECT query and uses and entirely different db handle altogether (same db, different Perl script included using a require)

# Takes the countryID as a parameter e.g. "12" sub getCountryNameFromID{ my $countryID = $_[0]; my $sql = qq{ SELECT fullcountryname FROM countries WHERE (country +ID="$countryID") }; $qry = $dbhutils->prepare($sql); $qry->execute(); @row = $qry->fetchrow_array; my $countryname = $row[0]; $qry->finish(); return $countryname; }

Bizzare?? Unexpected?? Any ideas anyone??

M

Note: the first time it goes through the loop it does the 5 "print $cgi->p(..." statements


In reply to Nesting SELECT statements by heezy

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.