Problem with filtered Mason code & db connection
ASE, DBI, Mason, Perl, Sybase, mod_perl Add commentsHi,
In chapter 5 (Advanced Features pgs 82,83) of Embedding Perl in HTML with Mason from O’Reilly, the"a simple SQL select expressed in something like a taglib style" example appears to be straight forward. It is but it doesn’t seem to work too well.
The premise is that the ".components/sql/select" will filter the chunk of html code
<&| .components/sql/select, query => ‘SELECT name, type FROM sysobjects’ &>
—-> here
<tr>
<td>%name</td>
<td>%type</td>
</tr>
—–>to here
</&>
The select returns data correctly but the ".components/sql/select" doesn’t appear to be printing the code hmmmm…. see the very bottom for the answer… I didn’t catch it for awhile but later saw the cause and could have kicked myself.
produced HTML code:
<head>
<title> Databases</title>
</head>
<body>
<table border="1">
<tr>
<th>Server</th><th>Service Type</th>
</tr>
</table>
<center><img src="/images/seal.png" height="170" width="170"/></center>
</body>
</html>
test.html:
<tr>
<th>Server</th>
<th>Service Type</th>
</tr>
< &| .components/sql/select, query => ‘SELECT name, type FROM sysobjects’ &>
<tr>
<td>%name</td>
<td>%type</td>
</tr>
</table>
.components/sql/select:
$query
< %init>
my $sth = $dbh->prepare($query);
while (my $row = $sth->fetchrow_hashref) {
my $content = $m->content;
$content =~ s/%(w+)/$row->{$1}/g;
$m->print($content);
}
$sth->finish;
Solution:
my $sth = $dbh->prepare($query);
while (my $row = $sth->fetchrow_hashref) {
should be:
< %init>
my $sth = $dbh->prepare($query);
$sth->execute(); # dang typos in the book Cry
while (my $row = $sth->fetchrow_hashref) {






Recent Comments