CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: DBI:
problem passing the $sth variable to another sub



Jul 8, 2010, 2:56 PM

Post #1 of 2 (6513 views)
problem passing the $sth variable to another sub Can't Post

Hello there,

I've got a problem passing the $sth variable to another sub:

this is the sub that gives problems:


sub display_items {
my ($sth, $min, $max) = @_;
if (!$min){
if (!$max){
} else {
$min = $max;
$max = $max + 50;
my $counter = 0;
my $previous_url = sprintf("%s?choice=previous&min=%i&max=%i&sth=%s", url(), $min, $max,$sth);
my $next_url = sprintf("%s?choice=next&min=%i&max=%i&sth=%s", url(), $min, $max,$sth);
print "this procedure is called $min $max $sth";
while (my $row = $sth->fetchrow_hashref()){
if ($min <= $counter && $counter <= $max) {
display_entry ($row);
if ($counter == 0){
print p("no items were found in the database"), br();
print br (), a({-href => $previous_url}, "previous")," ", a({-href => $next_url}, "next"), br();

When i call this sub with the following code it works just fine:


sub submit_param {
my ($dbh ,$search, $operator, $searchvalue) = @_;
my $sth;
$sth = $dbh->prepare ("SELECT * FROM squid WHERE $search = ? ORDER BY id");

but when i call it from the internetl link that i'm printing in the display_items sub, I get an error.

This is the code that gets called when clicking on the link:


}elsif ($choice eq "next"){
display_items(param("sth"), param("min"), param("max"));

this gets printed on the website:

this procedure is called 50 100 DBI::st=HASH(0x85da454)

this is the error i see in the apache log file:

[Fri Jul 09 01:50:45 2010] [error] [client] main::update_url() called too early to check prototype at /var/www/localhost/cgi-bin/ line 32., referer:
[Fri Jul 09 01:50:45 2010] [error] [client] Can't locate object method "fetchrow_hashref" via package "DBI::st=HASH(0x85da454)" (perhaps you forgot to load "DBI::st=HASH(0x85da454)"?) at /var/www/localhost/cgi-bin/ line 118., referer:

and this is the error i get when i call ./ choice=next

Use of uninitialized value in sprintf at ./ line 115.
Use of uninitialized value in sprintf at ./ line 116.
Use of uninitialized value in concatenation (.) or string at ./ line 117.
Can't call method "fetchrow_hashref" on an undefined value at ./ line 118.


Jul 15, 2010, 5:13 PM

Post #2 of 2 (6329 views)
Re: [lukdk] problem passing the $sth variable to another sub [In reply to] Can't Post


$sth is a hash "reference" (HASH(0x85da454)). As soon as your script ends (i.e. navigating to another page), the data that the $sth reference "linked to" is lost and cannot be accessed.

You can only post / get "strings" and not "perl data structures". Your options are to build the data structure into a string which you can later parse or to use a different method i.e. sessions, which have the ability to hold perl data structures.

However, there should be no reason why you would need to pass $sth via param etc. You should simply execute a fresh query to the database having only passed i.e. $searchvalue via param.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives