Hey Monks,

I asked the question below at a mysql forum but didn't get any replies. So I'm trying my luck here - at the risk of being down-voted.

The code below checks for the existence of a username. If one already exists, the user is informed to choose another one.

sub check_exist { $username = shift; # do the necessary dbi conection... $sql = qq{ SELECT * FROM $table{'members'} WHERE nick="$username"}; $sth = $dbh->prepare($sql); my $result = $sth->execute() or bail_out("Cannot execute query."); $dbh->disconnect(); if ($result eq '0E0') { return $username; } else { bail_out("$username has already been taken..."); } }
What I'm not so certain is the test for existence part. What's the right way to test for the existence (or non-existence) of a particular query string?

Updated:Thanks to Zaxo, mpeppler and CountZero! I implemented UNIQUE INDEX on the username and that itself prevents duplicate names. As a result, the check_exist sub has become redundant - but it was good for learning purposes. I don't think the book teaches you all this stuff!


In reply to perl (mysql) question... by kiat

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.