moosey_man1988 Posted May 13, 2015 Share Posted May 13, 2015 Hi There I'm fairly new to PHP and I'm currently working on a small website for testing purposes. I am working on a customer form I was previously using the auto_increment function in mysql to in order to generate a customer ID but I want a prefix on the beginning and you cant use VARCHAR with auto increment. I want to be able to create a unique ID incrementing by 1 with a prefix e.g. fill in the form and this generates a customer number of CN00001 then the next time you do it it will make CN00002, how is this possible, if you can point me in the right direction I would be very grateful. Many Thanks Mooseh Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/ Share on other sites More sharing options...
Barand Posted May 13, 2015 Share Posted May 13, 2015 (edited) Store the prefix in its own column with an auto_incrementing numeric id. To select SELECT CONCAT(prefix, id) as custno, ..... edit: and make the id column INT(5) ZEROFILL Edited May 13, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/#findComment-1511745 Share on other sites More sharing options...
moosey_man1988 Posted May 13, 2015 Author Share Posted May 13, 2015 oh yes that sounds like a great idea! how would i generate the 0's infront so i always have 2 characters along with 5 digits (for tidiness). Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/#findComment-1511746 Share on other sites More sharing options...
Barand Posted May 13, 2015 Share Posted May 13, 2015 see the edit to my post Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/#findComment-1511747 Share on other sites More sharing options...
moosey_man1988 Posted May 13, 2015 Author Share Posted May 13, 2015 thank you, quickest and easiest answer ever known on a forum lol. sometimes i just takes that different head on a pair of shoulders to see something differently. Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/#findComment-1511748 Share on other sites More sharing options...
Muddy_Funster Posted May 13, 2015 Share Posted May 13, 2015 (edited) Persoanly never been a huge fan of zerofill I would suggest SELECT CONCAT(prefix, LPAD(id,5,0)) as custno, ..... But that's a personal thing Edited May 13, 2015 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/296292-creating-a-unique-number/#findComment-1511749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.