
hwnd
User
Feb 21, 2013, 6:34 AM
Post #1 of 4
(163 views)
|
|
Help with MySQL
|
Can't Post
|
|
This code works fine for me to connect to the database and with using the template system but when it prints the output from getting the records from the database it is only printing the last record in that database instead of looping and returning all records. Any advice?
My template file holds this information for the output: <TMPL_LOOP NAME=DB_RECORDS> <tr> <td class="therecord"><TMPL_VAR NAME=ID></td> <td class="therecord"><TMPL_VAR NAME=DATE></td> <td class="therecord"><TMPL_VAR NAME=AUTHOR></td> <td class="therecord"><TMPL_VAR NAME=HEADLINE></td> <td class="therecord"><TMPL_VAR NAME=NEWS></td> </tr> </TMPL_LOOP> I am trying to fetch the records with fetch but it is only returning the last record:
while ( $sth->fetch ) { $template->param(DB_RECORDS => [ { id => $news_id, date => $news_date, author => $news_author, headline => $news_headline, news => $news_text }, ]); } Entire code: #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); use DBI; use HTML::Template; my $template = HTML::Template->new(filename => 'templates/newsboard.tmpl'); my $dbh = DBI->connect('DBI:mysql:updatesdb:mysql-updatesdb.liveforfaith.com', '*****', '*****') or die $DBI::errstr; my $query = "select id, date, author, headline, news FROM news"; my $sth = $dbh->prepare($query); $sth->execute(); my ($news_id, $news_date, $news_author, $news_headline, $news_text ); $sth->bind_columns( undef, \$news_id, \$news_date, \$news_author, \$news_headline, \$news_text ); while ( $sth->fetch ) { $template->param(DB_RECORDS => [ { id => $news_id, date => $news_date, author => $news_author, headline => $news_headline, news => $news_text }, ]); } print header, $template->output;
|