Before you insert into the database, you can run a simple test like this (I handle query's slightly differently):
# This is untested -- no perl here my $sth = $dbh->prepare("SELECT name FROM members WHERE username = ?") +; $sth->execute($username); # this replaces the '?' above if ($sth->rows != 0) { my $user = $sth->fetchrow_array; print "This username is in use by $user. Try another username.\n"; } else { $sth = $dbh->prepare("INSERT INTO member (username, password, email, + name, url, date) VALUES (?, ?, ?, ?, ?, ?)"); $sth->execute($username, $password, $email, $name, $url, $time); print "User Created Normally"; }
The first query is used to see if anyone exists in the database with that name. If it does not, it will return 0 rows ($sth->rows), and the user is created. If rows are returned, an error is put out.

Now you might ask why I handle queries that way. I use question marks for two primary reasons. One, I don't have to worry about punctuation so much. This helps me to avoid bugs. Second, I do this so that I can define all my queries at the top of a subroutine ($sth_user, $sth_articles, etc). I might define more than one, because I might try to nest some of my queries within while loops and if statements. It helps me to maintain clean code in the long run. Your method isn't incorrect...just that I find my method more logical for what I do. (Proof that there is 100 ways to do 1 thing).

Hope it helps.

--Coplan


In reply to Re: mySQL with Perl by Coplan
in thread mySQL with Perl by skirrow

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.