Jump to content
Sign in to follow this  

How to generate Auto Increment ID? (Character and Number)

Recommended Posts

Hi, how to generate auto increment ID with combination of characters and number.

For example, i would like to have product ID start from PROD00001.

When i add second product, the ID will check on the existing database to check PROD00001 is the latest ID.

So, the second product ID will +1, become PROD00002.


Share this post

Link to post
Share on other sites

So what exactly prevents you from using an actual AUTO_INCREMENT column? This “PROD...” stuff is only how you would display the numeric ID, and it can easily be done using (s)printf():


$id = 5;
printf('The ID is PROD%05d', $id);

In any case, don't try to invent your own auto-increment mechanism. If you naively select the latest ID, increment it and then update the ID, you'll get duplicate IDs when two PHP processes run at almost the same time.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • 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.