使用mysql_num_rows时未定义的变量

时间:2013-02-18 19:38:02

标签: php mysql

我遇到变量问题。看起来我必须在使用之前始终初始化$ variable。这非常恼人。  $ username_check = mysql_num_rows($ sql_username_check);这里它说通知:未定义的变量:c://中的username_check 注意:未定义的变量:c://中的email_check 我正在检查db中的重复数据。它不幸地添加了相同的数据:( 有人可以帮我这个。

// Database duplicate Fields Check
        $sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
        $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");
        if($sql_username_check != "")
            if(isset($username_check))
                $username_check= mysql_num_rows($sql_username_check);
        if($sql_email_check != "")
            if(isset($email_check))
                $email_check = mysql_num_rows($sql_email_check); 
        if ($username_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
        } 
        else if ($email_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
        } 

4 个答案:

答案 0 :(得分:0)

你有一些非常奇怪的代码,但你可以试试这个:

    $sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
    $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");

    if($sql_username_check)
      $username_check= mysql_num_rows($sql_username_check);

    if($sql_email_check)
      $email_check = mysql_num_rows($sql_email_check); 

    if (isset($username_check) && $username_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
    } else if (isset($email_check) && $email_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
    } 

答案 1 :(得分:0)

问题似乎从这里开始:

$sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");

函数调用不返回字符串,因此下面的比较不会按照您的想法执行。导致错误的行可能是:

if ($username_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
    } 
    else if ($email_check > 0){ 
        $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
    }

根据我从您的示例中读到的内容,我可以想到$username_check$email_check将在此设置的任何方式,以便您不会收到这些通知。)

您真正需要做的是了解如何使用数据库中的结果集,包括数据库错误处理。

在阅读本文时,您应该继续使用已弃用的mysql_*函数,并使用mysqliPDO了解最常用的功能PHP中使用的首选数据库库。

答案 2 :(得分:0)

if(isset($username_check))

这是在分配$username_check之前使用的,这就是为什么会有通知。

尝试这样的代码:

$sql_username_check = mysql_query("SELECT user_id FROM users WHERE user_username='$username' LIMIT 1");
        $sql_email_check = mysql_query("SELECT user_id FROM users WHERE user_email='$email' LIMIT 1");
        if($sql_username_check != "")
            $username_check= mysql_num_rows($sql_username_check);
        if($sql_email_check != "")
            $email_check = mysql_num_rows($sql_email_check); 
        if ($username_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside of our system. Please try another.";
        } 
        else if ($email_check > 0){ 
            $errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
        } 

答案 3 :(得分:-1)

您需要在PHP中调整错误报告设置。例如:

error_reporting (E_ALL & ~E_NOTICE & ~E_STRICT);