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.# 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"; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |