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

检查用户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";
    }
    
    已采纳该答案
    打赏 评论
  • douhao1956 2015-12-10 08:50

    your code has bugs, try this:

        $token  = "token";
     $data = json_decode(get_html("https://graph.facebook.com/$user->id&access_token=$token"))->data;
    
      $id = $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";
     }
    
    打赏 评论
  • doue1925 2015-12-10 08:50

    why the double check? if your sql returns a result the id exists.

    $id = $user->id;
    
    $result = mysql_query("SELECT * FROM token_all WHERE id = '" . $id . "'"); 
    if (mysql_num_rows($result) > 0)
    {
        echo 'TRUE';
    }
    else
    {
        echo 'FALSE';
    }
    
    打赏 评论
  • donglian1953 2015-12-10 09:20

    To check if id already exists in db you just need to check its count. Please check the following code.

     $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"); 
    
     $count_user=mysql_num_rows($result); // this will give you the count
    
     if($count_user >= 1 ) { // if this id is already present in the db
         echo "user already exists";
     }
     else {
         echo "user not exists";
     }
    
    打赏 评论

相关推荐 更多相似问题