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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



lukdk
Novice

Jul 8, 2010, 2:56 PM

Post #1 of 2 (3919 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:


Code
  

sub display_items {
my ($sth, $min, $max) = @_;
if (!$min){
$min=0;
}
if (!$max){
$max=50;
} 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);
}
$counter++;
}
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:


Code
  

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



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:


Code
  

}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 192.168.0.52] main::update_url() called too early to check prototype at /var/www/localhost/cgi-bin/test.pl line 32., referer: http://192.168.0.52/cgi-bin/test.pl?choice=update
[Fri Jul 09 01:50:45 2010] [error] [client 192.168.0.52] 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/test.pl line 118., referer: http://192.168.0.52/cgi-bin/test.pl?choice=update

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

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


Zhris
Enthusiast

Jul 15, 2010, 5:13 PM

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

Hey,

$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.

Chris

 
 


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

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