Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Get a hash object from a database with DBI for use with HTML::Template

by Cine (Friar)
on Aug 04, 2002 at 14:36 UTC ( [id://187489]=note: print w/replies, xml ) Need Help??


in reply to Get a hash object from a database with DBI for use with HTML::Template

A couple of comments on your code:
  • You should use placeholders for values where possible for many reasons, including security, problems with quoting etc. That is you should change
    my $stmt = "SELECT * FROM $table WHERE $key_col = '$key_val'"; my $sth = $dbhandle->prepare( $stmt ); $sth->execute();
    into
    my $stmt = "SELECT * FROM $table WHERE $key_col = ?"; my $sth = $dbhandle->prepare( $stmt ); $sth->execute($key_val);
  • You should errorcheck your database stuff. Add a || die $DBI::errstr to all your DBI->connect, $dbh->prepare and $sth->execute.
  • You only need two row, so only get those. That is change:
    my $ref = $sth->fetchall_arrayref(); $sth->finish(); return map { $_->[$name_col] => $_->[$val_col] } @$ref;
    into
    my $ref = $sth->fetchall_hashref([$name_col,$val_col]); $sth->finish(); return map {@$_} @$ref;
  • print "Content-Type: text/html\n\n"; is wrong. The HTTP standard says it should be \r\n\r\n. However most browsers dont care ;)
  • Dont use a & in a sub call without needing it. It overrides any prototype you may add at a later time to make perl check the number/type of params to get_hash.
  • Dont overgeneralize. If you only use get_hash for this specific purpose, then fill in the tablename/colname and select only the two rows you need instead of *.


T I M T O W T D I

Replies are listed 'Best First'.
(jeffa) 2Re: Get a hash object from a database with DBI for use with HTML::Template
by jeffa (Bishop) on Aug 04, 2002 at 17:12 UTC

    "Add a || die $DBI::errstr to all ... "

    Too much typing, especially after the code has already been typed. Just add {RaiseError => 1} to connect():
    my $dbh = DBI->connect( "dbi:mysql:mydb", "user", "pass", {RaiseError => 1}, );

    "Dont use a & in a sub call without needing it. It overrides any prototype you may add ..."

    Perl prototypes really are not very good, IMHO. I prefer to avoid them, opting for good documentation instead. Check out (tye)Re: A question of style for some good info.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://187489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-09-10 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (9 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.