Jump to content

[SOLVED] auto_increment for 3 digits?


NerdConcepts

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/55767-solved-auto_increment-for-3-digits/
Share on other sites

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

?>

Archived

This topic is now archived and is closed to further replies.

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