• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

create auto_increment function using letters instead of numbers

First of all, I'm danish, so sorry for my poor english.

Second; the question:

Most of you have probably heard of the auto_increment function in databases such as MySQL. Every new entry is automaticly given a number, which is one higher than the number before.

What if I need this function, but instead of numbers, it should be with letters.

I've come by a small javascript, which shows the generel idea:
www.firewerx.dk/tegn.htm (takes a few seconds to load)

What i need, is a way of using this script with a database. When i run the script, I need it to:
1: connect to the database (mysql).
2: select the latest added entry.
3: run the script, and making it "1" larger.

05:45 "abc" is added to the database by user#1
11:29 user#2 adds a new entry. The script connects to the database, finds "abc" (which was the latest entry added), adds "1" to "abc", which gives the string "abd". After which, "abd" is added to the database as a new entry.

The script needs to be using the following letters:
$letters = "abcdefghijkmnpqrstuvwxyz23456789"

The idea is, that after "a" comes "b", after "b" comes "c" [...] after "z" comes "2", after "2" comes "3" [...] after "9" comes "aa", after "aa" comes "ab", after "ab" comes "ac" etc etc etc

If you don't understand a word of this, please feel free to ask :-)

1 Solution
If you had it as 0123456789abcdefghijklmnopqrstuvwxyz
You could store it as a normal integer and convert it to base36 when you need it.
I'll try and come up with a way to do what you want, but it'd be easier if you switched to that. I know it's possible though and I'll get back to you.
exp_dkAuthor Commented:
I'm willing to give it a try, but the thing is, that I've not finally decided, whether or not I want the numbers included at all. I might reconsider, and just go with:
$letters = "abcdefghijkmnpqrstuvwxyz"

I'm not certain yet...

But feel free to let your thoughts wander of :-)
Marcus BointonCommented:
You can treat it as a base-36 number without worrying about the chars involved - there's nothing stopping from me counting in base 10 using "zu7s05khjt1" instead of 0-9. it really makes no difference, other than ignoring ordinal ASCII values of the chars. I posted some code in this question that allows you to convert from base 10 to and from any base you like, using any sequence of characters as symbols:


though this does use 0-9a-zA-Z, that's just the default in the digits function - just change the default string to whatever sequence of chars you like. These functions use the bcmath extension so you can use them to handle numbers of any size - I've used them on 160-bit hashes.

Don't use these values instead of autoincrement in mysql, use them as well as. Relations based on simple integers will always be faster than strings.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

exp_dkAuthor Commented:
I've posted this on another site like this; www.eksperten.dk (danish), and I've got hold on this script, which is doing exactly what I need it to do:

function spec_code($str, $char) {
    $strlenght  = strlen($str) - 1;
    $charlenght = count($char) - 1;
    $action    = true;
    $result    = '';

    while ($strlenght >= 0) {
        if ($action === true) {
            $key = array_search($str{$strlenght}, $char);
            if ($key < $charlenght) {
                $action = false;
                $result = $char[$key + 1] . $result;
            } else {
                $result = 'a' . $result;
        } else {
            $result = $str{$strlenght} . $result;
    if ($action === true) {
        return 'a' . $result;
    } else {
        return $result;

$char      = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
echo spec_code('zza', $char);


Thank you for trying, though


print $a;

Without numbers, that does it.  Quite simply.  It outputs abd.  If $a were zzz, it would output aaaa.  Just retreieve the last value from the database, put it in a variable and use the ++ operator on the variable.
exp_dkAuthor Commented:
arantius >>
That is true, however, it does not give me the opportunity to choose which letters to include in the returned string :-(
Closed, 250 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now