You have a potential for a whopping security hole here, depending on how this is used. With MySQL (unless this has changed recently) you can't execute multiple SQL statements with one query, so this hole doesn't apply, but I bring it up in case you need to port this code or some else tries to use it.

What happens if someone passes an $id with the following value:

$id = q|3';DROP TABLE people;SELECT * FROM people WHERE id = '3|;

Needless to say, that's going to ruin your day with many databases (the excess SQL is simply to ensure that this query parses correctly). The solution is to quote the $id or to use placeholders:

sub SelectProfile { my ( $id, $dbh ) = @_; $id = $dbh->quote( $id ); my $selectquery = "SELECT * FROM people WHERE id = $id"; my $sth= $dbh->prepare($selectquery); $sth->execute(); my $rowdata=$sth->fetchrow_hashref; return $rowdata; }

A couple of other things. One, why do you disconnect the database handle in this subroutine? That means someone trying to reuse this sub is in for a rude shock. This should be handled elsewhere.

The other issue is the "SELECT *...". This is usually bad practice. See (Ovid: Death to Select Star!) Re: MySQL DBI Help for details.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - potential security concern) Re: Passing database handles by Ovid
in thread Passing database handles by hakkr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.