在插入之前检查预先存在的记录的最快方法[mysql_errno()]

发布于 2024-12-27 19:01:57 字数 1287 浏览 3 评论 0原文

我的问题将使用电子邮件作为示例,但这可以适用于任何事物。


通常在注册新用户(包括插入他/她的电子邮件)之前,我会检查他/她的电子邮件是否已存在于数据库中,如下所示:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2025-01-03 19:01:57

在电子邮件字段上使用

INSERT IGNORE INTO Users VALUES(...);

唯一键,然后使用 mysql_affected_rows() 检查行数;

这将导致对数据库进行单个查询,并排除 SELECT 和 INSERT 之间时间窗口的竞争条件

Use

INSERT IGNORE INTO Users VALUES(...);

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

怪我鬧 2025-01-03 19:01:57

我相信 COUNT 是最快的方法之一。另请记住,在执行查询时,如果您只想测试其存在性,请不要执行“

SELECT *

而是指定特定字段”

SELECT id

,这样您就不会检索到那么多数据。在使用 COUNT 的情况下:

SELECT COUNT(id) FROM Users WHERE email = 'emailaddress'

请参阅下面的链接,了解如何在结果集中使用 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

SELECT *

Instead specify a specific field

SELECT id

So you are not retrieving as much data. In the case of using COUNT:

SELECT COUNT(id) FROM Users WHERE email = 'emailaddress'

Please see below link on how to use COUNT in a result set

http://www.tizag.com/mysqlTutorial/mysqlcount.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文