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

时间:2022-09-25 23:20:57

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

我想检查数据库print false中是否已存在User id

$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 个解决方案

#1


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.

如果您得到任何结果,只需进行计数并测试。如果你得到了结果,你已经在数据库中拥有了id,如果你没有得到任何结果,那么id就不存在了。

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

我也删除了$ id = echo“”。 $用户> ID;因为我们可以在查询中直接使用$ user-> id。

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

还有一件事,你应该避开mysql_ * api,因为它已被弃用并转向mysqli_ *或更好的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";
}

#2


0  

your code has bugs, try this:

你的代码有bug,试试这个:

    $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";
 }

#3


0  

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

为什么要仔细检查?如果你的sql返回结果,则id存在。

$id = $user->id;

$result = mysql_query("SELECT * FROM token_all WHERE id = '" . $id . "'"); 
if (mysql_num_rows($result) > 0)
{
    echo 'TRUE';
}
else
{
    echo 'FALSE';
}

#4


0  

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

要检查db中是否已存在id,您只需检查其计数。请检查以下代码。

 $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";
 }

#1


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.

如果您得到任何结果,只需进行计数并测试。如果你得到了结果,你已经在数据库中拥有了id,如果你没有得到任何结果,那么id就不存在了。

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

我也删除了$ id = echo“”。 $用户> ID;因为我们可以在查询中直接使用$ user-> id。

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

还有一件事,你应该避开mysql_ * api,因为它已被弃用并转向mysqli_ *或更好的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";
}

#2


0  

your code has bugs, try this:

你的代码有bug,试试这个:

    $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";
 }

#3


0  

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

为什么要仔细检查?如果你的sql返回结果,则id存在。

$id = $user->id;

$result = mysql_query("SELECT * FROM token_all WHERE id = '" . $id . "'"); 
if (mysql_num_rows($result) > 0)
{
    echo 'TRUE';
}
else
{
    echo 'FALSE';
}

#4


0  

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

要检查db中是否已存在id,您只需检查其计数。请检查以下代码。

 $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";
 }