My question will use emails as an example, but this could apply to anything.
Normally before registering a new user (including inserting his/her email) I check if his/her email already exists in the DB something like this:
$result = mysql_query("SELECT * FROM Users WHERE email = '".mysql_real_escape_string($email)"';");
if(!$result)
{
die(mysql_error());
}
if(mysql_num_rows($result) > 0)
{
die('<p>Email already in DB. <a....>Recover Email</a></p>');
}
else
{
//insert new user data into Users table
}
Since I'd have constrained the email
field in my Users table to be UNIQUE
anyway, wouldn't it be quicker to try to insert first and if it fails, check the error? Something like this:
$result = mysql_query(...);//insert new user data into Users table
if(!$result)
{
if(mysql_errno()==$insert_unique_error)
{
$error = '<p>Email already in DB. <a....>Recover Email</a></p>';
}
else
{
$error = mysql_error();
}
die($die_str);
}
The problem is that I don't know what $insert_unique_error
should be. These are the only error codes I could find:
The first two characters of an SQLSTATE value indicate the error class:
Class = '00' indicates success.
Class = '01' indicates a warning.
Class = '02' indicates “not found.” This is relevant within the context of cursors and is used to control what happens when a cursor reaches the end of a data set. This condition also occurs for SELECT ... INTO var_list statements that retrieve no rows.
Class > '02' indicates an exception.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…