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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|