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!