0
$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),对吗?

4

2 回答 2

0

上面的代码会起作用,if 基本上是在说 $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');
}
于 2013-10-05T04:16:41.883 回答
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')");
}
于 2013-10-05T04:31:52.827 回答