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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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

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