Re: Handling MySQL errors
| From: | John W. Holmes | Date: | Sat, 03 Jul 2004 15:30:09 +0000 |
| Subject: | Re: Handling MySQL errors | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-189632@lists.php.net to get a copy of this message | ||
Shaun wrote:
Is there a way to handle MySQL errors gracefully such as a duplicate entry
into a UNIQUE field, the method I've been using seems a little laborious...
//see if user exists
$query = "SELECT *
FROM users
WHERE Username = '".$_POST[username]."'";
$result = @mysql_query($query);
$num = @mysql_num_rows($result);
if($num > 0){
$error = "Username already exists, please choose a another one";
header("Location: add_user.php?error=$error");
exit;
}
What would be a graceful way to handle it, in your opinion? You have the mysql_error() and mysql_errno() functions. You can just do you're INSERT and then check for errno 1062 for a duplicate key and act accordingly. Define "graceful" for us.
--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/
php|architect: The Magazine for PHP Professionals – www.phparch.com