#Select fieldnames from accfields table in @fields.
my @fields = getMultiRows($sql,1,$dbh); ##A saperate perl routine to extract data.
my $fields = join(",",@fields);
$sql = "SELECT $fields FROM tran WHERE account=?";
my $sth = $dbh->prepare($sql);
if(!$sth){$err= "Error preparing query " . $sql . " : ". $dbh->errstr;}
if(!$sth->execute(177)){$err = "Error Executing query " . $sql . " : ". $dbh->errstr;}
my %rows = $sth->fetchrow_hashref;
# Now i get the html template and want to populate html form fields with data extracted. All form field names are same as table column name.
foreach(@fields){
$template->param($_=>$rows{$_});
}
return $template->output;
The form is displayed but fields do not show data extracted from database! Where am I making mistake? The html tag in form is -
Where am I making mistake?
In a number of ways:
You are not handling the possible error you store in $err.
Consult the DBI.pm documentation how to work with fetchrow_hashref.
You did not tell us much about the templating system to understand if there's anything wrong there.
Cheers, Sören
my $p = Page->new();
# now we get the info from the database and pass it to the template
# in this case, the template needs a reference to an array of hashes
# in order to handle the TMPL_LOOP properly. Which is exactly what
# dbselect returns. hard to believe :-)
$newst->param(news => $p->dbselect($sql,1));
sub dbselect{
my Page $self = shift;
my $sql = shift if @_;
my $hash = shift if @_;
my $db = $self->db();
my $h = $db->prepare($sql) or $self->errorpage("DB problem in select prepare: " . $db->errstr());
$h->execute() or $self->errorpage("DB problem in select execute: " . $db->errstr());
my $arrayref;
if ($hash){
$arrayref = $h->fetchall_arrayref({});
} else {
$arrayref = $h->fetchall_arrayref();
}
return $arrayref;
}
I'll break down what just happened there. I'm using an object called Page (I use it for web pages in general), it has a method called dbselect that will query the database for me (the connection is set up elsewhere). $newst is an HTML::Template that I have genereated, so I call the param method of $newst with the output from dbselect assigned to the TMPL_LOOP news. It then fills in my news page with rows of news. Some day I'll update it to limit the number of stories, but I'm basically boring so I don't have that much news to display :-)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
perlmonks.org content © perlmonks.org and bar10der, benrwebb, Happy-the-monk
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03