ID

I’m not sure if this post goes here or in the MySQL section, but I put it here because I am a newbie and I’m probably asking an obvious question. My question is, when a person registers on my site, I want a SECOND ID that they login with. I’ve asked this question before and people say, just use the auto_increment for the ID, but that’s not my purpose here. I’m wanting for, when a person to register, it automatically assigns the next number in order in the database and that’s what the person logs in with. For instance, a person registers and the last ID in the field is 042 (yes, the 0 in front is required). How could I get it to make the next entry be 043? What function would I use? Thanks in advance! And if anyone is wondering, the purpose behind this is a login system for a virtual airline. Pilot’s login using their pilot ID.

Hi there,

To get the most recent entry you should just be able to run this query:

SELECT `id` FROM `my_table` ORDER BY `id` DESC LIMIT 1

That will return the highest id in the table

Hi,

You can also use mysql_insert_id() function. After the insertion of your data, obtain the last inserted ID using that function and increment it by 1. Your code may look similar to this:

[php]$new_ID = mysql_insert_id() + 1;[/php]

Cheers.

Thanks! But how can I make the id a three digit number instead of just 1. (I want the first number to be 001.)

[php]$num = /whatever new id is/;
$id = $num < 10 ? “00”.$num : $num < 100 ? “0”.$num : $num;[/php]

Unless there’s an actual function out there?

What if I wanted to change the ID? How would that effect this?

Sponsor our Newsletter | Privacy Policy | Terms of Service