Hi guys...
I need to generate a 3-4 letter unique id and add it to a database.
I'm not sure how many characters there is, that I can use (I'm guessing around 128 or 256)... If there's around 100, 3 digits would be enough.
So, here'e what I need it for:
I have a php script that adds a records to a "log-database" each time it's activated. Now, I want to add a unique id also... I son't think I can use the "id" field from the mysql databse, because it's only numbers and that wouldn't be enough....
Also, I need the ID before I save the data to the database.
Any ideas?
Create a unique id and add that to mysql database
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Create a unique id and add that to mysql database
Why not create the row in the db, get the id using mysql_insert_id(), process whatever, then update the record?
Re: Create a unique id and add that to mysql database
My main problem here is generating the id. I would like to somehow, create an ascending letter id, like: aaaa, aaab, aaac, aaad, etc.John Cartwright wrote:Why not create the row in the db, get the id using mysql_insert_id(), process whatever, then update the record?
I was thinking that I could fetch the prior id from the database and then use a function (or something else?) to generate the next id in the line.
But I have absolutely no clue on how to do this
Re: Create a unique id and add that to mysql database
Okay... after some searching and messing around, I stumbled upon base_convert 
So, this is how I did it:
$letters = "aaaa";
$numbers = base_convert($letters, 36, 10);
$newnumber = $numbers+1;
$newletter = base_convert($newnumber, 10, 36);
echo $newletter;
will return: aaab
I know this is a no-brainer for a lot of you in here... but just wanted to share anyway, just in case
So, this is how I did it:
$letters = "aaaa";
$numbers = base_convert($letters, 36, 10);
$newnumber = $numbers+1;
$newletter = base_convert($newnumber, 10, 36);
echo $newletter;
will return: aaab
I know this is a no-brainer for a lot of you in here... but just wanted to share anyway, just in case