NerdConcepts Posted June 15, 2007 Share Posted June 15, 2007 In MySQL you've got the "auto_increment" setting. Well I cannot figure out how to make that a 3 digit number: 001,002,003...010,011... etc. Right now it's 1,2,3,4....10,11.... Just wondering if there is way to make it always 3 digits. I want to use that number to add it to tracking numbers for products and various activity. But it really needs to be 3 digits, easier to reference stuff. I don't care if it's I have to use PHP to insert it myself into mysql, but it needs to auto increment according to what available in the database. Quote Link to comment Share on other sites More sharing options...
chigley Posted June 15, 2007 Share Posted June 15, 2007 Well why not just store them as 1, 2, 3, 4 etc. then use PHP to format upon output? <?php // $number ~ the original number // $length ~ the length to pad the number to function format_number($number, $length) { $currentlength = strlen($number); $add = ($length - $currentlength) + 1; $arg = "%0{$add}d"; return sprintf($arg, $number); } echo format_number(1, 2); // 01 ?> Quote Link to comment Share on other sites More sharing options...
Nhoj Posted June 15, 2007 Share Posted June 15, 2007 Make the column an 'Unsigned Zero Fill' 'auto_increment' column in mysql and it will always pad the number with zeros. Also set the int type as something like SMALLINT(3). Quote Link to comment Share on other sites More sharing options...
akitchin Posted June 15, 2007 Share Posted June 15, 2007 you can also use mysql's LPAD() function when pulling the value, rather than relying on PHP to do it. Quote Link to comment Share on other sites More sharing options...
NerdConcepts Posted June 15, 2007 Author Share Posted June 15, 2007 Thanks a bunch. Nhoj: used your unsigned zerofill. Works perfectly, thanks a bunch. Quote Link to comment 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.