kiat has asked for the wisdom of the Perl Monks concerning the following question:
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.
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?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..."); } }
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl (mysql) question...
by Zaxo (Archbishop) on Dec 08, 2003 at 06:19 UTC | |
by CountZero (Bishop) on Dec 08, 2003 at 06:32 UTC | |
by kiat (Vicar) on Dec 08, 2003 at 07:03 UTC | |
by Zaxo (Archbishop) on Dec 08, 2003 at 18:36 UTC | |
by CountZero (Bishop) on Dec 08, 2003 at 23:24 UTC | |
|
Re: perl (mysql) question...
by Roger (Parson) on Dec 08, 2003 at 05:54 UTC | |
|
Re: perl (mysql) question...
by perlcgi (Hermit) on Dec 08, 2003 at 14:07 UTC | |
|
Re: perl (mysql) question...
by mpeppler (Vicar) on Dec 08, 2003 at 16:10 UTC |