Jump to content

Globally Unique IDs


hackalive

Recommended Posts

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
Share on other sites

"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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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" :P

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
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.