检查用户ID已经存在于数据库Mysql中(Check user id Already exist in database Mysql)

我想检查数据库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"; }

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

最满意答案

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

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

还有一件事,你应该避开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"; }

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

更多推荐