Error with logging users on facebook
I have an app on facebook, and I log the users on my database (a script checks if the user is already on the db, if not, it adds him). The users table has only one field - "userid", which is a primary key.
The script that checks if the user is already registered in the db is:
$selectuser = $db->query("SELECT * FROM `predictorusers` WHERE `userid`='$user'");
if ($db->countRows($selectuser) == 0)
$db->query("INSERT INTO `predictorusers` (`use开发者_如何学编程rid`) VALUES ('$user')");
Now to the problem: I don't know why, but with a specific user (for most users if works just fine) it inserts the id 2147483647 (which doesn't even exist on facebook!!), and when he enters the app again, he gets the following message:
Duplicate entry '2147483647' for key 1
Like it's trying to add it again even though it already exists in the db!!
Can anyone figure it out?
It is regarding the definition of the field in MySQL. I am sure your field is set to be of INT type.
Duplicate entry '2147483647' for key 1 == maximum number for this type in MySQL.
So when a FB ID is larger than this, MySQL will set this as the maximum permitted, thus giving you duplicates when the ID is large, as the entry must be unique.
Change the field to VARCHAR(255) and your problem will be resolved.
ALTER predictorusers
ALTER COLUMN userid
VARCHAR(255) NULL
use BIGINT (20) instead as the datatype. The FB IDs are too big.
精彩评论