如何在mysql中检查两个值

时间:2015-01-30 12:48:52

标签: php mysql

我正在使用mysql。

我想检查users表中的两个值。

$same_user = "mfalse" ;

$sql=mysql_query("SELECT * FROM users WHERE username='$email' OR phone='$phone'");
        if(mysql_num_rows($sql)>=1)
        {
            $same_user = "mfalse" ;
        }
        else
        {
            $same_user = "mtrue" ;
            mysql_query("SET CHARACTER SET utf8");
            mysql_query("insert into users (username,pass,fname,lname,phone,city)
              VALUES  (N'$email',N'$pass',N'$fname',N'$lname',N'$phone',N'$city')");
        }


        $arr = array(
        'sameuser'=>$same_user);

    return $arr ;

在上面我的代码中,插入发生但是从$arr我得到:

  

{ “sameuser表示”: “mfalse”}

为什么?


更新

我的功能:

function regiterAnUser($an_user){
    //email,pass,fname,lname,phone,city
    $content    = explode("-",$an_user);
    $email      = $content[0];
    $pass       = $content[1];
    $fname      = $content[2];
    $lname      = $content[3];
    $phone      = $content[4];
    $city       = $content[5];

    $pass = md5($pass);


    $same_user = "mfalse" ;

    $sql=mysql_query("SELECT * FROM users WHERE username='$email' OR phone='$phone'");
    if(mysql_num_rows($sql)>=1)
    {
        $same_user = "mfalse" ;
    }
    else
    {
        $same_user = "mtrue" ;
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("insert into users (username,pass,fname,lname,phone,city)
          VALUES  (N'$email',N'$pass',N'$fname',N'$lname',N'$phone',N'$city')");
    }


    $arr = array(
        'sameuser'=>$same_user);

    return $arr ;
}

2 个答案:

答案 0 :(得分:1)

我不会使用mysql功能,但是,这是你的选择。

我重构了你的代码,它对我有用,请让我知道

function regiterAnUser($an_user){
    $same_user = "mfalse" ;
    //email,pass,fname,lname,phone,city
    $content    = explode("-",$an_user);
    $email      = mysql_real_escape_string($content[0]);
    $pass       = mysql_real_escape_string($content[1]);
    $fname      = mysql_real_escape_string($content[2]);
    $lname      = mysql_real_escape_string($content[3]);
    $phone      = mysql_real_escape_string($content[4]);
    $city       = mysql_real_escape_string($content[5]);
    $pass = md5($pass);

    $sql=mysql_query("SELECT * FROM users WHERE username='$email' OR phone='$phone'");
    if(mysql_num_rows($sql)<1) {
        $same_user = "mtrue" ;
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("insert into users (username,pass,fname,lname,phone,city)
          VALUES  (N'$email',N'$pass',N'$fname',N'$lname',N'$phone',N'$city')");
    }

    return array('sameuser'=>$same_user) ;
}

答案 1 :(得分:0)

您是否尝试输出$sql

无论如何,我建议用MySQL计算行数。看here

请不要使用mysql_*个功能。它们已被弃用。例如,请参阅here