Jump to content

Adding auto numbers


daneth1712

Recommended Posts

Hi,

 

I need to add into my database a field that inserts consecutive numbers starting from a 8 digit number I have selected.

I need to be able to add the next number automatically to each row in the database (there are over 100,000) and then add some php code so that when a new entry is added, it adds the next number to the new record.

 

The problem is I already have an auto increment field in the database, and am not sure how to do this.

 

Can someone please help me out?

 

Thanks in advance.

 

Link to comment
Share on other sites

Hi

 

Only idea I can suggest is to have a 2nd table with a single column which is an autonumber (starting at your start point), and when you want a new number you insert into that and then grab the inserted key value for use in your main table.

 

All the best

 

Keith

Link to comment
Share on other sites

What's wrong with using a standard auto-increment INT field and start it at whatever value you want. It's true that it won't be padded to 8 characters, but that is a display issue. By keeping the field as an INT type you get much more flexibility; e.g. select where field > [some_value].

 

Then whenever you want to display the field simply use string_pad() or some other methos to display with it padded to eight characters

Link to comment
Share on other sites

What's wrong with using a standard auto-increment INT field and start it at whatever value you want.

 

The problem is I already have an auto increment field in the database, and am not sure how to do this.

Link to comment
Share on other sites

Hi guys,

 

Thanks for your help, but I managed to do it another way.

 

I added the code below in case anyone is interested.... just have to add script to insert next number into new record, which is the easy bit.

 

<?php
$d="10085201";

$db = mysql_connect("$host", "$user", "$pass") or die ("Error connecting to database.");
mysql_select_db("$database", $db) or die ("Couldn't select the database.");
$sql = "select id from $users ORDER by id asc";
$result=mysql_query($sql,$db) or die(mysql_error()); 

while($row=mysql_fetch_array($result)) { 
$reg=$row['id']; 
$sql2= "update $users SET d_id = '$d' WHERE id='$reg'";
$d ++;
$result2=mysql_query($sql2 ,$db) or die(mysql_error());
}
?>

Link to comment
Share on other sites

Hi Kevin,

 

This script was only meant to be used once, all I needed to do was add a consecutive number to each record on the database. now I have done that, the script above is going to be binned. I will now use another bit of code to make sure the last record is checked and next number is added to new record.

 

Thanks for everyones help! :)

Link to comment
Share on other sites

What's wrong with using a standard auto-increment INT field and start it at whatever value you want.

 

The problem is I already have an auto increment field in the database, and am not sure how to do this.

 

OK, so use THAT field for this number. Just start the auto-increment from the number you need to start at and, as I stated above, use PHP to format the number appropriately for display purposes. Maybe I am missing something, but it would seem that two auto-increment fields for a table are superfluous.

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.