AidanLee has asked for the wisdom of the Perl Monks concerning the following question:

Well, I've done some more delving into this problem I was having this weekend and found that I was not looking in the right place. The Template Toolkit is not to blame, and instead i have found a few odd behaviors I'd like to do away with:

Running in a CGI environment (MySQL,Win32 w/ Xitami web server) my script queries the database and returns a list of the records found. The odd thing is that when the number of records changes in the database, or the Publish field (boolean 1|0, see code below) is toggled on a record in the table, the returned data does not reflect this. i.e., it still returns the same entries it did the last time.

Here's the wierd part. It does not return the correct set of records until I modify the source code of the script and resave. Then it correctly retrieves the records once. If i drop into the database and twiddle the Publish field in a record again, we're back to not returning the right set of records.

As a final kick in the but, the script runs without a hitch at the command line.


The code, which I posted before, but which has changed slightly. This has not altered it's behavior (still running under -w):

#------------------------------------- # PRAGMAS use lib 'e:/web_projects/marksphotos/lib'; use strict; use utf8; #------------------------------------- # IN HOUSE MODULES #use Config; use HTML::Tools; use dbWrapper qw( QueryDB ); #------------------------------------- # 3RD PARTY MODULES use Template; #===================================================================== +===== # MAIN #===================================================================== +===== my %params = ParseQueryString(); my $template = Template->new({INCLUDE_PATH => 'e:/web_projects/marksph +otos/templates/',DELIMITER=>';',COMPILE_EXT=>'c'}); my %db_info = (); my $page = ''; #------------------------------------- my $list_limit = ( exists $params{limit} and $params{limit} =~ /^(\d ++)$/ ) ? $1 : 10; my $list_offset = ( exists $params{offset} and $params{offset} =~ /^(\ +d+)$/ ) ? $1 : 0; my $date_format = '%a %M %d, %Y | %l:%i %p'; for( 'log' ) # normally this is for( $params{view} ) { /log/ and do { $db_info{query} = "SELECT ID,DATE_FORMAT( +Date_Added,'$date_format') as Created,DATE_FORMAT(Date_Modified,'$dat +e_format') as Modified,Subject,Body FROM LogEntries WHERE Published=' +1' ORDER BY Created LIMIT $list_offset,$list_limit"; $db_info{fields} = [ qw( ID Date_Added Da +te_Modified Subject Body) ]; $db_info{table} = 'LogEntries'; $page = 'log/list.t'; last; }; /announcements/ and do { $db_info{query} = "SELECT ID,DATE_FORMAT( +Date_Added,'$date_format') as Created,DATE_FORMAT(Date_Modified,'$dat +e_format') as Modified,Body FROM Announcements WHERE Published='1' OR +DER BY Created LIMIT $list_offset,$list_limit"; $db_info{fields} = [ qw(ID Date_Added Dat +e_Modified Body) ]; $db_info{table} = 'Announcements'; $page = 'announcements/list.t'; last; }; /collections/ and do { $db_info{query} = "SELECT C.ID,DATE_FORMA +T(C.Date_Added,'$date_format') as Created,DATE_FORMAT(C.Date_Modified +,'$date_format') as Modified,C.Title,C.Summary,P.Filename as Image FR +OM PhotoCollections C LEFT JOIN Photos P ON C.Feature_Image = P.ID W +HERE Published='1' ORDER BY Created DESC LIMIT $list_offset,$list_lim +it"; $db_info{fields} = [ qw(ID Date_Added Dat +e_Modified Title Summary Image) ]; $db_info{table} = 'PhotoCollections'; $page = 'collections/list.t'; last; }; do{ print FrontPage(); exit(0); }; } #------------------------------------- if( $db_info{table} ) { my $sth = undef; #-- sanity check for any available records ---------------------- { $sth = QueryDB("SELECT COUNT(ID) from ".$db_info{table}." WHERE Pu +blished='1'"); } my( $count ) = $sth->fetchrow_array; if( $count > 0 ) #-- there are records to fetch ---------------------------------- { if( $count > $list_offset ) #-- fetching a set of records in a valid range ----------------- +- { $sth = QueryDB($db_info{query}); my($arrayref,%hash) = ( undef ); push @{$arrayref},{ %hash } while( @hash{ @{$db_info{fields} +} } = $sth->fetchrow_array() ); #-- successfully retrieved records ------------------------- +----- if( scalar( @{$arrayref} ) ) { # DEBUG TEST ------------------------ print '<div style="position:absolute;top:500px;" /><pre> +'; foreach( @{$arrayref} ) { print "\n\n"; while( my( $key,$value ) = each %{$_} ) { print "$key: $value\n"; } } print "</pre></div>\n"; # DEBUG TEST ------------------------ my $result = ''; $template->process($page,{entries=>$arrayref, list => { limit=>$list_limit, offset=>$list_offset +, total=>$count }, text => { crop => \&crop_text, nl2br => \&nl2br } }, \$result ) or die $template->error(); print $result,$count; exit(0); } else #-- couldn't retrieve records ------------------------------ +----- { print ErrorMessage("There has been an internal error") a +nd exit(0); } } else #-- tried to get set of records off the end of the table ------- +- { print ErrorMessage("You've tried to get a list of entries t +hat don't exist") and exit(0); } } else #-- there aren't any records to fetch --------------------------- { print ErrorMessage("There are no entries for display") and exit +(0); } } #===================================================================== +===== # SUBS #===================================================================== +===== sub FrontPage { return ErrorMessage("welcome to the front page"); } sub ErrorMessage { my $text = ''; $template->process('error.t',{message=>$_[0]},\$text ) or die $tem +plate->error(); return $text; } #===================================================================== +===== sub crop_text #a second return value of '1' indicates that the string was truncated. { my($text,$limit) = @_; $text =~ /^(.{1,$limit}\W)/s; return ($text,0) if( $text eq $1 ); return ($1,1); } # convert newlines to line breaks sub nl2br { my $text = $_[0]; $text =~ s|\n|<br />|g; return $text }
  • Comment on Change in Source changes records returned in an unchanged query...
  • Download Code

Replies are listed 'Best First'.
Re: Change in Source changes records returned in an unchanged query...
by physi (Friar) on May 22, 2001 at 01:06 UTC
    I might be wrong, but this seems to be the cache of your browser.
    Have you tried to reload the page (CTRL-R in netscape) ??
    This would also explain, why it works on the comandline.

    Hope this helps.

    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      I wish it were that simple. No, I've been reloading the page to see changes.
        Do you use a proxy? How about Shift-Reload? But I think it's the Server. He caches the script and all Variables. The Variables contain a cache for the Database. The cache for Database doesn't correctly get the change. The script thinks it's internal cache is valid. Unless the Server sees the script changed and reloads the script thereby initialising the internal cache to "not valid".