Phll
Phll

Reputation: 71

PHP - Create GUID from ID

I'm wondering if there's a way to create a GUID from a MySQL record ID, so that each time a GUID is created from that ID, it will be the same.

For instance, if we have an ID of 347, the method would look something like this:

create_guid_from_id(347);

always returns: 90880842-7b30-46b0-8179-fa876d4d84bd

Any ideas?

Upvotes: 1

Views: 2493

Answers (1)

Aaron Long
Aaron Long

Reputation: 93

GUIDs are probably not the right thing you are after here. As they produce different output every time and in general don't accept input values

As the commenters have posted you are better to use a hashing algorithm. Hashing algorithms are deterministic/idempotent, meaning that the algorithm will produce the same output every time as long as the input is the same.

You can use md5("347") in order to do this in PHP. Note that the md5 function takes a string as a parameter so probably best to convert your id to a string first

http://php.net/manual/en/function.md5.php

It's also worth noting the hashes can't be converted back into their original form. So you can never get your id back from the hash.

Upvotes: 3

Related Questions