4

I've written a registration page in PHP using prepared statements. My question is a fairly simple one. When a user registers I want to make sure that they aren't registering under the same username or email as another person. Therefore as part of my verification procedure I run two queries, one to check if the email exists and one to check if the username exists. My question is, is there a better method? I feel that running more that one query could somehow sacrifice some performance. Will performance even be noticably affected by such a query? I realise that I could run "SELECT * FROM table WHERE username = ? or email = ?", but I would like the user to know what they got wrong rather than just a generic "Either your username or email is currently being used, feel free to use trial and error to find which it is". It's probably me just being paranoid about speed, but I was just interested to see what everyone else's opinion on the matter is.

4

6 回答 6

3

使用 OR 运行查询并检查返回的结果以报告您找到的确切内容。

但是,根据数据库内部结构和索引,两个单独的查询可能比 OR'ed 一起更快。您最好对所有案例(找到一个、两个都找到、没有找到)进行基准测试,以获得有关您的环境的明确答案。

于 2012-07-03T11:48:35.180 回答
2

在您保存用户信息的数据库中,使用户名和电子邮件实体唯一。就像主键一样。

于 2012-07-03T11:55:27.100 回答
0

您的查询很好;您将有 1 或 2 个结果;2 如果存在仅使用电子邮件或仅使用昵称的注册;1 如果注册有邮箱和昵称;使用JS并在输入框右侧指定原因

于 2012-07-03T11:51:46.667 回答
0

好吧,我想这将是最简单的方法:

SELECT IF(username = :username_to_check, 1, 0) AS dup_username
  FROM users
 WHERE username = :username_to_check OR email = :email_to_check

然后检查: 1) 行数 - 如果为 0,则未找到重复项;2) 结果的 dup_username 标志,如果行数 > 0。

这是一个可以玩的例子。

于 2012-07-03T11:51:46.990 回答
0

我不认为它对性能不利

如果您有从表中获取一行的选择查询;让我们说数千行;那么影响就更不用说了。。

什么影响您的性能是连接查询需要大量资源,而您的情况没有发生......

我相信单独使用它是安全的,我们一直在我们的网站上这样做,目前拥有超过一百万用户

还尝试在您的 mysql 服务器中运行此查询,看看这需要多长时间才能有所帮助(尤其是对于更复杂的查询)

于 2012-07-03T11:52:05.410 回答
0
    $result = mysql_query("SELECT * FROM table1 WHERE Username = ''", $link);
    $num_rows = mysql_num_rows($result);

    $result2 = mysql_query("SELECT * FROM table1 WHERE Email= ''", $link);
    $num_rows2 = mysql_num_rows($result2);

    $checkUsrN = false;
    $checkEmail = false;
    $mesg = "The following is bad: ";

    if($num_rows == 0)
    {
       $checkUsrN = true;         
    }
    else{
        $mesg .= "Username";
    }

    if($num_rows2 == 0)
    {
       $checkEmail = true;
    }
    else{
        $mesg .= "Email";
    }

    if($checkUsrN == false || $checkEmail == false)
    {
       echo $mesg;
       break;
    }

你的目的是什么?

于 2012-07-03T11:53:24.133 回答