hackalive Posted June 3, 2012 Share Posted June 3, 2012 Hi guys, Quick one (I hope). How could I replicate Facebook's allocation of a unique ID to a vast range of resources (https://developers.facebook.com/docs/reference/api/) With Facebook every photo, person, event, page has a unique ID. If I wanted to make every page have a unique ID all I would need to do is have a page table with ID that auto increments. But this is not what I want to achieve, I want to achieve a system like Facebook has, where every object ("resource") has a unique ID. Cheers in advance Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/ Share on other sites More sharing options...
smoseley Posted June 3, 2012 Share Posted June 3, 2012 "Globally unique" means unique around the world (to all applications on all computers everywhere). You can get a GUID using com_create_guid(). If you only want something unique to your application, you can just use uniqid($type, true) - where $type is the type of uniqid you want to generate, e.g. "image", "user", etc. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350852 Share on other sites More sharing options...
hackalive Posted June 3, 2012 Author Share Posted June 3, 2012 I mean globally unique in terms of my DB - just like Facebook. i.e., There cannot be a page id = 1, event id = 1, person id = 1 Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350853 Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 Then you need something somewhere that tracks all the ID numbers. Like a database table with an auto_increment field, then all the other tables's primary keys are actually FKs to that table (and don't have auto_increment themselves). query("INSERT INTO global_id_table (fields that help identify what this ID is for) VALUES (information or description or something)"); $id = get insert ID(); query("INSERT INTO real table (ID, fields) VALUES ($id, values)"); Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350961 Share on other sites More sharing options...
smoseley Posted June 4, 2012 Share Posted June 4, 2012 Those aren't globally unique... you mean just "unique". Again, "global" has worldwide scope. "Unique" has application scope. I don't think FB uses unique IDs, FYI. If you want unique uris, just use the prefix in the URI: domain.com/user/1 = user #1 domain.com/photo/1 = photo #1 Why complicate it? Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350973 Share on other sites More sharing options...
requinix Posted June 4, 2012 Share Posted June 4, 2012 Those aren't globally unique... You mean you think OP actually meant literally global? Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350987 Share on other sites More sharing options...
smoseley Posted June 4, 2012 Share Posted June 4, 2012 Those aren't globally unique... You mean you think OP actually meant literally global? I don't think he meant that, but he said that. Words have meanings. When you use the wrong words, you convey the wrong meaning. Globally Unique ID (GUID) has a well-defined meaning in application development. If you don't mean global, don't say global. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1350990 Share on other sites More sharing options...
kicken Posted June 4, 2012 Share Posted June 4, 2012 MySQL has a function which will return a 64bit integer unique id that you could use in your application. There is also a function that returns a 128 Universal Unique Identifier. UUID_SHORT() UUID() You would just use that function in your insert queries instead of (or in addition to) an auto_inc column, eg: INSERT INTO photos (PhotoID, UserID, Filename) VALUES (UUID_SHORT(), $UserId, $Filename) If you needed to know the value of the ID for the new row to do additional operations you'd want to select the id first, eg: $query = 'SELECT UUID_SHORT() as id'; $res = /* Run $query */ $row = /* Fetch from $res */ $query = 'INSERT INTO photos (PhotoID, UserID, Filename) VALUES ('.$row['id'].', $UserId, $Filename)'; ///and so on Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351012 Share on other sites More sharing options...
requinix Posted June 4, 2012 Share Posted June 4, 2012 Words have meanings. When you use the wrong words, you convey the wrong meaning. Globally Unique ID (GUID) has a well-defined meaning in application development. If you don't mean global, don't say global. And on the other hand there's terms like "global variable" and "global scope"... Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351024 Share on other sites More sharing options...
smoseley Posted June 4, 2012 Share Posted June 4, 2012 Words have meanings. When you use the wrong words, you convey the wrong meaning. Globally Unique ID (GUID) has a well-defined meaning in application development. If you don't mean global, don't say global. And on the other hand there's terms like "global variable" and "global scope"... Which have their own meanings. "Globally Unique Identifier" or GUID, (the title of this thread) is a term invented by Microsoft, which is an ID that uses your NIC's MAC address, the time + micro-time, and a random sequence to generate it. Thus, it represents an absolutely unique identifier. Want to learn more? https://www.google.com/search?q=globally+unique+id Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351093 Share on other sites More sharing options...
Jessica Posted June 4, 2012 Share Posted June 4, 2012 And if you read beyond the title, you can easily tell the OP is not talking about GUID. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351200 Share on other sites More sharing options...
Zane Posted June 4, 2012 Share Posted June 4, 2012 Why not, instead of autoincrement, you just use an MD5 substring, using the current time and the users username as the parameters to be hashed. When you insert a new row, check for errors of duplication, and if there is a duplicate, hash it again with a salt... repeat. Because seeing as you want this "global" id within the scope of your database, checking for duplication is the secret. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351201 Share on other sites More sharing options...
Jessica Posted June 4, 2012 Share Posted June 4, 2012 How is that easier than an incrementing number? Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351202 Share on other sites More sharing options...
Zane Posted June 4, 2012 Share Posted June 4, 2012 I never said it was easier, just more..."global" Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351208 Share on other sites More sharing options...
smoseley Posted June 4, 2012 Share Posted June 4, 2012 And if you read beyond the title, you can easily tell the OP is not talking about GUID. Which is why, in my first post, I both educated the OP on the correct meaning of GUID and offered a solution to his actual problem. And the thread SHOULD have ended there.... I never said it was easier, just more..."global" Or you could just use the solution I suggested in the first reply and have a truly "global" id that is easy to create. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351220 Share on other sites More sharing options...
KevinM1 Posted June 4, 2012 Share Posted June 4, 2012 Well, it looks like this thread has run its course. If you want to continue to bitch at each other, take it to PMs. Link to comment https://forums.phpfreaks.com/topic/263588-globally-unique-ids/#findComment-1351223 Share on other sites More sharing options...
Recommended Posts