0

可能重复:
mysql_fetch_array() 期望参数 1 是资源,选择中给出的布尔值

//define variable to check the number of rows where the sql query is true to find out if the username or password are already in use
$EmailCheck = mysql_query("SELECT `Email` FROM `Customer` WHERE `Email` = '$RegCusEmail'");
$UserCheck = mysql_query("SELECT `Username` FROM `Customer` WHERE `Username` = '$RegCusUser'");

//checks if emails in use
if (mysql_num_rows($EmailCheck) != 0)
{
  echo '<p id="error">The Email address you have entered is already in use.<p>';
}

//checks if username is in use
elseif (mysql_num_rows($UserCheck) != 0)
{
  echo '<p id="error">The username you have entered is already in use.<p>';
}

上面的代码应该在数据库中搜索注册人的电子邮件和用户名,如果它们已经存在,告诉用户使用不同的,但是当我使用它时,我得到了这个错误?

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in (file directory) on line 56 
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in (file directory) on line 62
4

4 回答 4

1

1)使用PDO:

//connection
$host =     "localhost";
$user =     "someuser";
$pass =     "somepass";
$database = "db_name";

$db = new PDO("mysql:host=$host;dbname=$database", $user, $pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die("Connection error!");

$query = 'SELECT Customer.Email FROM Customer WHERE Customer.Email = :RegCusEmail';
$params = array(':RegCusEmail' => $RegCusEmail);

$result = db_query($query,$params);

if($result == false)
{
  //do stuff..
}


function db_query($q,$p = array())
{
    global $db;

    if($stmt = $db->prepare($query))
    {

        if(count($p) > 0)
        {
            foreach ($p as $key => $value) {
                $stmt->bindParam($key,$value);
            }
        }

        $stmt->execute();
        $resulSet = $stmt->fetchAll(PDO::FETCH_ASSOC);

        if(count($resulSet) > 0)
        {
            return true; // exists
        }
        else
        {
            return false; // does not exist
        }

    }

}

2) 验证电子邮件地址和电子邮件服务器

function isValidEmail ($email)
{
    if ($isemail=filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        if(list($email,$domain) = explode('@',$email))
        {
            if(!getmxrr ($domain,$mxhosts)) 
            { 
                return FALSE; 
            }
            else 
            { 
                return TRUE; 
            }
        }
        else
        {
            return FALSE;
        }
    }
    else
    { 
    return FALSE; 
    } 

}
于 2013-01-18T15:56:35.543 回答
0

改为对结果进行检查:

if ( ! is_resource( $EmailCheck ) || mysql_num_rows( $EmailCheck ) != 0 )

但是检查一下mysql_error(),您的查询可能有错误!

于 2013-01-18T15:49:24.000 回答
0

您似乎忘记了连接数据库,或者数据库中的详细信息可能不正确。当您遇到查询错误时,最好通过将查询die (mysql_error())作为错误处理方法结束来进行调试,如下所示:

mysql_query("SELECT Email FROM Customer WHERE Email = '$RegCusEmail'") or die (mysql_error());

得到一个正确的错误。

于 2013-01-18T15:45:05.387 回答
0

如果您使用的是 5.2 或更高版本,它就像

 function checkEmail( $EmailCheck ){
    return filter_var( $EmailCheck, FILTER_VALIDATE_EMAIL );
  }

显然添加错误处理..

于 2013-01-18T15:47:07.370 回答