views:

296

answers:

4

I've been working a little bit on a Facebook application, but when I registered a new user to test the friend interaction the new user got a uid (100000XXXXXXXXX) that seems to big for php to handle.

Saving the number in the database results in the same value (2147483647). I'm guessing this is also PHPs fault, as I would believe the uid would fit in an unsigned bigint?

I'm not quite sure where to go from here, any suggestions?

+7  A: 

The fix is to store the UID as a string always. Use the VARCHAR field type in MySQL and you will be fine.

In general, many database gurus will tell you that interpreting another application's foreign keys (like UID in this case) is bad bad bad and you should handle them as opaque text.

intgr
Using the TEXT field type is a bad idea, especially if this data needs to be indexed. A varchar(xx) would be a lot more sensible.
middaparka
Egad, TEXT is *not* the way to go for this. You should be using a VARCHAR instead. When you know the length of the data you want to store, using a TEXT type should always be avoided.
Scott Anderson
+1 for varchar. A text field (as opposed to a numeric) is the way to go, but not a 'text' type field.
Tim Lytle
Sorry, I come from the Postgres world where text and varchar are the same thing.
intgr
+1 for varchar. Did the first 2 commenters just read the last word of your entry?
marr75
No, my post was wrong to begin with. I corrected it from the feedback. :)
intgr
It was helpful no matter, problem solved. Thank you :)
Marco
+1  A: 

I'm using BIGINT UNSIGNED for a couple applications and it works just fine.

Peter Bailey
And use strings in PHP to store the UID.
Stefan Gehrig
But what does PHP do when you query this data from the database? On 32-bit platforms, PHP's integer datatype can only fit values up to 2147483647.
intgr
Apparently PHP handles the numbers fine on 64 bit systems, from what I just read. Does this apply to your server?
Marco
Relying on the bit width of a certain web server is surely a bad idea.
intgr
PHP gets the values from the database as `string`s - there is no automatic type conversion happening here. All data that's retrieved from the database are `string`s.
Stefan Gehrig
@Stefan that is correct.
Peter Bailey
A: 

What type of MySQL field are you storing the UID data in? An unsigned bigint can store up to 18446744073709551615. (See http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html)

Simply update your schema via something like...

ALTER TABLE <table name> MODIFY COLUMN <column name> BIGINT UNSIGNED NOT NULL;

...and I suspect all will be well.

NB: You'll want to try this on a backup to ensure I'm not deeply incorrect. :-)

middaparka
+4  A: 

Facebook recommonds to store it as a BIGINT unsigned (http://wiki.developers.facebook.com/index.php/User%5FID).

For PHP, you'd store it as a string (because, ultimately, if you're going to use it, it's going to be displayed on the page or in JSON data or something else that's stringy. There's no real need to perform arithmetic on that number).

Shawn Leslie
and... comparison operators within the database for an integer will be much more effecient than comparisons on a string.
Shawn Leslie
+1 for referencing the Facebook developer wiki, which has had this question answered since February 2008.
R. Bemrose
@Bemrose I wouldn't call that a question answered, I was using an unsigned bigint, my problem was that PHP couldn't handle the number as a number, so saving it broke it :)
Marco