<tfoot id='An8Yr'></tfoot>

    <small id='An8Yr'></small><noframes id='An8Yr'>

        <bdo id='An8Yr'></bdo><ul id='An8Yr'></ul>
    1. <legend id='An8Yr'><style id='An8Yr'><dir id='An8Yr'><q id='An8Yr'></q></dir></style></legend>

        <i id='An8Yr'><tr id='An8Yr'><dt id='An8Yr'><q id='An8Yr'><span id='An8Yr'><b id='An8Yr'><form id='An8Yr'><ins id='An8Yr'></ins><ul id='An8Yr'></ul><sub id='An8Yr'></sub></form><legend id='An8Yr'></legend><bdo id='An8Yr'><pre id='An8Yr'><center id='An8Yr'></center></pre></bdo></b><th id='An8Yr'></th></span></q></dt></tr></i><div id='An8Yr'><tfoot id='An8Yr'></tfoot><dl id='An8Yr'><fieldset id='An8Yr'></fieldset></dl></div>
      1. 在插入 [mysql_errno()] 之前检查预先存在的记录的最快方法

        时间:2023-10-30
      2. <small id='C7s1U'></small><noframes id='C7s1U'>

          <bdo id='C7s1U'></bdo><ul id='C7s1U'></ul>
          <legend id='C7s1U'><style id='C7s1U'><dir id='C7s1U'><q id='C7s1U'></q></dir></style></legend>

            <i id='C7s1U'><tr id='C7s1U'><dt id='C7s1U'><q id='C7s1U'><span id='C7s1U'><b id='C7s1U'><form id='C7s1U'><ins id='C7s1U'></ins><ul id='C7s1U'></ul><sub id='C7s1U'></sub></form><legend id='C7s1U'></legend><bdo id='C7s1U'><pre id='C7s1U'><center id='C7s1U'></center></pre></bdo></b><th id='C7s1U'></th></span></q></dt></tr></i><div id='C7s1U'><tfoot id='C7s1U'></tfoot><dl id='C7s1U'><fieldset id='C7s1U'></fieldset></dl></div>
            <tfoot id='C7s1U'></tfoot>

                  <tbody id='C7s1U'></tbody>

                  本文介绍了在插入 [mysql_errno()] 之前检查预先存在的记录的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的问题将以电子邮件为例,但这可能适用于任何事情.

                  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
                  }
                  


                  既然我已经将我的用户表中的 email 字段限制为 UNIQUE 无论如何,尝试先插入会不会更快,如果它失败,检查错误?像这样的:


                  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);
                  }
                  

                  问题是我不知道 $insert_unique_error 应该是什么.这些是我能找到的唯一错误代码:

                  The problem is that I don't know what $insert_unique_error should be. These are the only error codes I could find:

                  SQLSTATE 值的前两个字符表示错误类别:

                  The first two characters of an SQLSTATE value indicate the error class:

                  Class = '00' 表示成功.

                  Class = '00' indicates success.

                  Class = '01' 表示警告.

                  Class = '01' indicates a warning.

                  Class = '02' 表示未找到".这与游标的上下文相关,用于控制游标到达数据集末尾时发生的情况.SELECT ... INTO var_list 不检索任何行的语句也会出现这种情况.

                  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.

                  类 >02"表示异常.

                  Class > '02' indicates an exception.

                  推荐答案

                  使用

                  INSERT IGNORE INTO Users VALUES(...);
                  

                  在电子邮件字段上使用唯一键,然后使用 mysql_affected_rows() 检查行数;

                  with a unique key on email field, then check row count with mysql_affected_rows();

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

                  This will result in a single query to the DB and rule out the race condition of the time window between SELECT and INSERT

                  这篇关于在插入 [mysql_errno()] 之前检查预先存在的记录的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么在将二进制数据从 PHP 插入 MySQL 时使用 bin2hex? 下一篇:在远程数据库中插入 134675 个值的最快方法

                  相关文章

                  <legend id='6dUam'><style id='6dUam'><dir id='6dUam'><q id='6dUam'></q></dir></style></legend>
                  <i id='6dUam'><tr id='6dUam'><dt id='6dUam'><q id='6dUam'><span id='6dUam'><b id='6dUam'><form id='6dUam'><ins id='6dUam'></ins><ul id='6dUam'></ul><sub id='6dUam'></sub></form><legend id='6dUam'></legend><bdo id='6dUam'><pre id='6dUam'><center id='6dUam'></center></pre></bdo></b><th id='6dUam'></th></span></q></dt></tr></i><div id='6dUam'><tfoot id='6dUam'></tfoot><dl id='6dUam'><fieldset id='6dUam'></fieldset></dl></div>
                  <tfoot id='6dUam'></tfoot>

                      <bdo id='6dUam'></bdo><ul id='6dUam'></ul>
                    1. <small id='6dUam'></small><noframes id='6dUam'>