my $table = 'book'; my $condition = '1'; my $sort_field = 'id'; my $sort_order = 'DESC"; my $sql = "SELECT id, name, location FROM $table WHERE $condition ORDER BY $sort_field $sort_order"; print $sql,"\n"; my $sth = $dbh->prepare($sql); $sth->execute() or warn "Unable to query $table" $dbh->errstr . "\n" . $!; while (my $row = $sth->fetchrow_hashref()) { print $row->{'id'},"\t",$row->{'name'},"\t",$row->{'location'},"\n";; }