You might wonder how to make perl work something similar to php or even asp when querying DB, well here is a code that might help you.
Php as well as asp they allow you to call fields from a record set in the way $rs['field_name'] and perl some times seem to be verydifucult... then this might change your point of views. take a look a t this:
#!/usr/bin/perl
use CGI;
use DBI;
$dbname = "";
$host = "";
my $db = 'DBI:mysql:$dbname:$host';
my $username = 'user';
my $pass = 'password';
my $dbh = DBI->connect($db, $username, $pass);
my $sql = "SELECT * FROM categoria";
$sth = $dbh->prepare($sql);
$sth->execute() or die "Error";
my($count) = $sth->rows();
print "Records: $count\n";
my %row;
$sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } ));
while ($sth->fetch) {
foreach $key (keys (%row)){
print "\$$key - $row{$key} ** ";
}
print "\n";
}
$sth->finish;
$dbh->disconnect();
exit;
This will code will let you call record set field streight from the query.. supose you have a field name id then you can cal $row{'id'} within the foreach loop.
Hope this is usefull, comments are welcomed..
No comments:
Post a Comment