PHP / Mysql--查找重复的电子邮件但我的代码中有错误

时间:2014-02-21 02:42:48

标签: php mysql

检查电子邮件是否已存在..

$query="SELECT email FROM user_info WHERE email='".$Email."'";
    $result=mysql_query($query) or die("email cannot be added".mysql_error());
    if($row=mysql_fetch_array($result))
        {
        die(" Sorry that user " .$email. " already exists ! <br>");
        }

请给我简单的程序

2 个答案:

答案 0 :(得分:1)

$query = "SELECT email FROM user_info WHERE email = '" . $email . "'";
$result = mysql_query($query);

if(mysql_fetch_array($result) !== false) {
    die("Sorry that user " . $email . " already exists ! <br>");
}

您还应该知道,PHP变量区分大小写,因此如果未定义其中一个,则使用$Email$email将失败。

答案 1 :(得分:0)

尝试使用mysql_num_rows

$query="SELECT email FROM user_info WHERE email='".$email."'";
$result=mysql_query($query) or die("email cannot be added".mysql_error());
if(mysql_num_rows($result) > 0)
{
    die(" Sorry that user " .$email. " already exists ! <br>");
}