$check_select = mysql_num_rows(mysql_query("SELECT * FROM `fblogin` WHERE email = '$user_email'"));
if($check_select > 0)
mysql_query("INSERT INTO `fblogin` (fb_id, name, email, image, postdate) VALUES ('$user_fbid', '$user_fnmae', '$user_email', '$user_image', '$now')");
我从网上某处获取的代码,我只是好奇if($check_select > 0)
是否正确,我认为它应该是if($check_select != 0)
,对吗?
答案 0 :(得分:0)
如果$ check_user大于0,那么上面的代码将起作用,这与$ check_user不等于0的情况相同。
之前我使用过这段代码,这就是我用它做的:
这是基于您创建新用户
你必须在Mysql_query中连接你的数据库,我建议使用mysqli,而不是mysql:
// CONNECT TO THE SERVER
$sql_con = mysqli_connect(host,username,password,dbname);
// RUN THE QUERY ON YOUR DATABASE
$check_user = mysqli_query($sql_con, "SELECT * FROM `fblogin` WHERE email = '$user_email'");
// GET NUMBER OF USERS FOUND
$check_user_num = mysqli_num_rows($check_user);
// IF NO USER EXISTS IN DATABASE DO THE FOLLOWING
if($check_user_num = 0) {
mysql_query("INSERT INTO `fblogin` (fb_id, name, email, image, postdate) VALUES ('$user_fbid', '$user_fnmae', '$user_email', '$user_image', '$now')");
} else {
// ELSE DO THIS CODE HERE (for me, I normally redirect with an error code, like the following:)
header('location:?error=exists');
}
答案 1 :(得分:0)
$conn=mysqli_connect("hostname","username","password");
$check_select = mysqli_query("SELECT * FROM `fblogin` WHERE email = '$user_email'");
$numrows=mysqli_num_rows($check_select);
if($numrows > 0){
mysqli_query("INSERT INTO `fblogin` (fb_id, name, email, image, postdate) VALUES ('$user_fbid', '$user_fnmae', '$user_email', '$user_image', '$now')");
}