Help with creating unique ID in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Help with creating unique ID in PHP

Post by pido »

Hi need help how to make an unique id to auto number.
basically i want my id like this Q-$id/$month/$year when its printed it goes Q-01/11/2005.

Question is how do i make the $id + 1 everytime i save the query, first i have to check previous query id right?? and put the $id into string and check the $id?? But i want every first day of new month the $id change to 01 and so on...

How i do that??? anyone can help me figure out this problem?? Thnx...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

set your ID to primary key and auto_increment.. that will guarantee the ID will be unique every time.
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Post by pido »

I know that, but the point is not that simple, my Inquery # is in this format Q-$id/$month/$year (Q-01/11/2005) <-- Characters not a Integer or Number so i can't use auto_increment.

How i make the $id adding 1 everytime i save my Inquiry and change to 01 again every first day of new month???
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Is this mysql?


itd be a matter of

Code: Select all

SELECT `id` FROM `table` WHERE `month` = $current_month ORDER BY `id` DESC LIMIT 1
to get the last insert id for the current month
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

$query = "SELECT MAX(ID) FROM users";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$id = mysql_fetch_array($result);

$id = $id['MAX(ID)']+1;
This is what i use. : p

-NSF
User avatar
pido
Forum Commoner
Posts: 35
Joined: Mon Jan 12, 2004 8:03 am

Post by pido »

Thnx for the help guys, but let me make this clear,
I have table tbl_inquiry -> PK_inquiryID,inqDate,....
value of PK_inquiryID is Q-001/11/2005, remember 01 is not a day its a number increment for month 11. It could be like this Q-115/11/2005.

Big Question is how i make the PK_inquiryID increase since its a char type. Should i put the PK_inquiryID in a string first and convert it into integer??? And how do i restart the $id to 001 every first day of new month???? eg. Q-001/12/2005.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Fetch the latest autonumber id from the database, add 1, then

Code: Select all

$big_id = "Q-".$id."/".$month."/".$year
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

That's poor data control. (For a number of reasons, one that you are experiencing now..)

Seperate the ID and the date.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Jcart wrote:set your ID to primary key and auto_increment.. that will guarantee the ID will be unique every time.
No it won't. If someone were to do "truncate table tablename;" then the auto increment field would be reset. Then you'd get orders with the same id as old orders as they're inserted. It's usually good enough, but it'd be wrong to assume it's guaranteed unique. If you really need that guarantee, use either an MD5 checksum of the incremental id, the date, and some text, or use MySQL's UUID() function.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

It would probably make more sense to generate your ID using the auto_encrement, then to use md5(uniqid(1)), the main reason being md5(unqid(1)) still allows for collisions, and also with the auto ID your order numbers actually mean something.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

Just create following table

Letterabreb char(2) - for your Q-
ID int autoincrement primary - for the number
month char(2)
year char(4)
delimiter char(1) - for the /

index on ID month year

now you got all seperated. You can hardcode the delimiter into the query if you like and delete the table
Post Reply