在插入之前检查预先存在的记录的最快方法[mysql_errno()]
我的问题将使用电子邮件作为示例,但这可以适用于任何事物。
通常在注册新用户(包括插入他/她的电子邮件)之前,我会检查他/她的电子邮件是否已存在于数据库中,如下所示:
$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
}
因为我已经限制了用户表中的电子邮件
字段无论如何,要UNIQUE
,先尝试插入,如果失败,检查错误不是更快吗?像这样的事情:
$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);
}
问题是我不知道$insert_unique_error应该是什么。这些是我能找到的唯一错误代码:
SQLSTATE 值的前两个字符指示错误类别:
Class = '00'表示成功。
Class = '01' 表示警告。
Class = '02' 表示“未找到”。这与游标上下文相关,用于控制游标到达数据集末尾时发生的情况。对于未检索行的 SELECT ... INTO var_list 语句,也会出现这种情况。
类> ‘02’表示异常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在电子邮件字段上使用
唯一键,然后使用 mysql_affected_rows() 检查行数;
这将导致对数据库进行单个查询,并排除 SELECT 和 INSERT 之间时间窗口的竞争条件
Use
with a unique key on email field, then check row count with mysql_affected_rows();
This will result in a single query to the DB and rule out the race condition of the time window between SELECT and INSERT
我相信 COUNT 是最快的方法之一。另请记住,在执行查询时,如果您只想测试其存在性,请不要执行“
而是指定特定字段”
,这样您就不会检索到那么多数据。在使用 COUNT 的情况下:
请参阅下面的链接,了解如何在结果集中使用 COUNT
http:// /www.tizag.com/mysqlTutorial/mysqlcount.php
I believe that COUNT is one of the quickest methods. Also remember when doing your queries, if you only want to test its existence don't do a
Instead specify a specific field
So you are not retrieving as much data. In the case of using COUNT:
Please see below link on how to use COUNT in a result set
http://www.tizag.com/mysqlTutorial/mysqlcount.php