doujiu8145 2015-12-10 08:34
浏览 59
已采纳

检查用户ID已经存在于数据库Mysql中

I want to Check if User id is already exist in database print false

$token  = "token";
 $data = json_decode(get_html("https://graph.facebook.com/$user->id&access_token=$token"))->data;

  $id = echo "".$user->id;
  $result = mysql_query("SELECT * FROM token_all WHERE id = $id"); 
 while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
 $checkid = row[id];
  }
  if ($id == $checkid){
echo "true";
 }else{
echo "false";
 }
  • 写回答

4条回答 默认 最新

  • dongwuxie5112 2015-12-10 08:49
    关注

    Just do a count and test if you get any results.
    If you get results you already have the id in the database,
    if you don't get any results the id is not there.

    Also I removed $id = echo "" . $user->id; since we can use $user->id directly in the query.

    One more thing, you should steer away from the mysql_* api since it has been deprecated and move towards mysqli_* or better PDO.

    $token  = "token";
    $data = json_decode(get_html("https://graph.facebook.com/$user->id&access_token=$token"))->data;
    
    $result = mysql_query("SELECT * FROM token_all WHERE id = " . $user->id); 
    $count = mysql_num_rows($result); // edited here
    
    if ($count > 0){
        echo "ID already exists";
    }else{
        echo "ID doesn't exist";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?