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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |