我正在使用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 ;
}
答案 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)