Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Undefined Value Error Message

by Milti (Beadle)
on Mar 14, 2014 at 18:58 UTC ( [id://1078385]=note: print w/replies, xml ) Need Help??


in reply to Re: Undefined Value Error Message
in thread Undefined Value Error Message

Dave, you were right but there is another problem. Here's the entire code.

#/usr/bin/perl -w use strict; use DBI; use CGI qw(:standard); use POSIX qw(ceil floor); my $LastName=param('LastName'); my $reqpage= param('reqpage'); unless ($reqpage > 1) {$reqpage = "1"; &count; } if ($reqpage > 1) { &search; } sub count { my $dbh = DBI->connect('dbi:mysql:membersdb','member','memberpass +wd') or die "Connection Error: $DBI::errstr\n"; # Count how many rows are there in a table, so that we can use it +for $pagenum. my $LastName=param('LastName'); my $n="3"; my $LastName= substr($LastName,0,$n); my $query="$LastName\%"; my $sql = "select count(*) from members WHERE (Location = 'Phoenix +, AZ USA' and LastName LIKE ?)"; my $sth = $dbh->prepare ("$sql"); $sth->execute($query) || quit(); my $result_count = $sth->fetchrow_array; $sth->finish; my $pagesize = "5"; if ($result_count != 0) { my $pagecount = int($result_count / $pagesize); if (($pagecount * $pagesize) != $result_count) { $pagecount++; } $sth->finish; $dbh->disconnect (); &search; sub search { my $dbh = DBI->connect('dbi:mysql:membersdb','member','memberpass +wd') or die "Connection Error: $DBI::errstr\n"; my $reqpage= param('reqpage'); unless ($reqpage > 1) {$reqpage = "1";} my $pagesize = "5"; my $firstresult = (($reqpage - 1) * $pagesize) + 1; my $lastresult = $firstresult + $pagesize - 1; if ($lastresult > $result_count) { $lastresult = $result_count; } my $offset = (($reqpage-1) * $pagesize); my $limit = "5"; my $result; my @result; my $LastName=param('LastName'); my $n="3"; my $LastName= substr($LastName,0,$n); my $query="$LastName%"; my $sql = "Select AccountID,LastName,FirstName,Location,Specialty,Intr +oduction,View from members WHERE (Location = 'Phoenix, AZ USA' and La +stName LIKE ?) Limit $offset,$limit"; my $sth=$dbh->prepare("$sql"); $sth->execute($query) or die "Connection Error: $DBI::errstr\n"; print header(), start_html; my $page = "E:/companyname/website/htdocs/resources/header_1.htm"; open (PAGE, "$page") || die "Couldn't open $page"; while (<PAGE>) {print;} print "<br><p align=\"center\"><font size=\"5\"><b>&nbsp;&nbsp;<u>Your + Search Results</u></b></font><br><br>"; while (my @results = $sth->fetchrow_array) { my $AccountID = $results [0]; my $LastName = $results [1]; my $FirstName = $results [2]; my $Location = $results [3]; my $Specialty = $results [4]; my $Introduction = $results [5]; my $View = $results [6]; my $Image = $results [0]; print <<"(END TABLE HTML)"; <center> <Table width="700" style="border-collapse: collapse;border: 1px solid +#000000" bordercolor="#111111"> <tr><td width="90" valign="top"><img src="http://www.website.com/resou +rces/$Image" height="100" width=100" alt = "No Image Available"></td> +<td align="left"><p align=\"left\"><font size=\"4\"><ul><li><b>Name&n +bsp;----&nbsp;$FirstName&nbsp;$LastName</b><br>Location&nbsp;----&nbs +p;$Location<br> Specialty&nbsp;----&nbsp;$Specialty<br><br>$Introduction<br><br>$View< +/li></ul></font></p> </td></tr></table> (END TABLE HTML) ##++$count; } my $prev_page; my $next_page; my $prev_link; my $next_link; my $pagelinks; my $pageno; my $thislink; my $pagesize = "5"; $prev_page = $reqpage - 1; $next_page = $reqpage + 1; if ($reqpage == 1) { $prev_link = ""; } else { $prev_link = "<a href=\search_test14.pl?reqpage=$prev_page>Previous</a +>&nbsp;"; } if ($reqpage == $pagecount) { $next_link = ""; } else { $next_link = "<a href=\search_test14.pl?reqpage=$next_page>NEXT</a>"; } if ($pagecount > 1) { $pagelinks = $prev_link; $pageno = 0; while ($pageno < $pagecount) { $pageno++; if ($pageno == $reqpage) { $thislink = " <b>$pageno</b> "; } else { $thislink = "<a href=\search_test14.pl?reqpage=$pageno>$pageno</a>&nbs +p;" } $pagelinks = $pagelinks . $thislink; } $pagelinks = $pagelinks . " " . $next_link; print "<br><p align=\"center\"><font size=\"3\">$pagelinks</font></p>" +; } else { $pagelinks = ""; } } print <<"(END FOOTER HTML)"; <head> <title>FOOTER</title> </head> <body topmargin="0" leftmargin="0" rightmargin="0"> <table width="800"> <tr> <td width="800"><p align="left"><a href="http://www.diversitylink.com" +>Home</a></p> </td> </tr> <tr> <td width="100%"> <img src="http://www.website.com/images/blk.gif" height="1" widt +h="100%"></td> </tr> </table> <table width="800" cellpadding="0"><tr><td width="100%"><p align="cent +er"><font size="1" face="Arial"> © My Company<br> All Rights Reserved<br></font></td> </tr></table> (END FOOTER HTML) end_html (); $sth->finish; $dbh->disconnect (); } } exit;
I am trying to get results from a table in the MySQL database and show 5 results per page. The initial search does just that having determined the correct number of pages required, and shows links 1-2-3-etc-Next on the bottom of the page. However when page 2, 3, or Next are called the $Name value is ignored and everything in the column 'Location' is counted again and everything is presented with the correct number(5 rows) per page. Why is the name value being ignored?? Help Please!!!

Replies are listed 'Best First'.
Re^3: Undefined Value Error Message
by graff (Chancellor) on Mar 15, 2014 at 03:15 UTC
    Your code looks like spaghetti. I was actually surprised that it compiles, but I'm not surprised to see these warnings:
    "my" variable $LastName masks earlier declaration in same scope at 107 +8385 line 32. Variable "$result_count" will not stay shared at 1078385 line 63. "my" variable $LastName masks earlier declaration in same scope at 107 +8385 line 78. "my" variable $pagesize masks earlier declaration in same scope at 107 +8385 line 124. Unrecognized escape \s passed through at 1078385 line 133. Variable "$pagecount" will not stay shared at 1078385 line 136. Unrecognized escape \s passed through at 1078385 line 140. Unrecognized escape \s passed through at 1078385 line 152. 1078385 syntax OK
    The cases of '"my" variable … masks earlier declaration' will probably mess things up.

    The "unrecognized escapes" are cases where you seem to have an unnecessary backslash (or maybe you intended to put \" but didn't?)

    The variables that "will not stay shared" happen because you're defining the "search" sub inside the scope of defining the "count" sub, the variables in question are declared (lexically scoped) inside "count", before sub search begins, and then they're being used inside "search".

    That's a strange (and wrong) way to lay out subroutines. For example, consider what happens at start-up if the "reqpage" parameter you get is greater than 1: the "search" sub will be called without the "count" sub being called first, but the "$result_count" variable is only assigned a value (from a query) in the "search" "count" sub, which hasn't been called in this case. That would probably explain the problem you're describing.

    There's also a missing double-quote at line 111 (the over-long line in the "heredoc"; this doesn't affect compiling, but it'll screw up your page display. (I noticed that because I used emacs with colorized text, and after that line, the coloring was inverted for code vs. string values).

    So let me suggest:

    • FIX YOUR INDENTATION so that it is consistent with the actual structure of the code.
    • Move all of sub search { … } so it's completely outside of sub count { … }
    • Figure out how the "unshared" variables in "search" (which are now undeclared) are going to be assigned the values that they're supposed to have when the script runs with reqpage > 1
    • Maybe google around for "paging database query results in perl cgi" - I think it should be pretty easy to find some useful examples to work from.
    When you see how other people handle paging, you'll probably want to start over on this script.

    (Updated to get the sub names right in the middle paragraph, and a grammar blot in the suggestion list.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1078385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-09-17 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.