0

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";
 }
Anil
  • 3
  • 4

4 Answers4

0

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";
}
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
0

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";
 }
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
0

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';
}
Bjoern
  • 23
  • 6
0

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";
 }
Nehal
  • 1,542
  • 4
  • 17
  • 30