Page 1 of 1

Help with creating unique ID in PHP

Posted: Wed Nov 23, 2005 5:16 pm
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...

Posted: Wed Nov 23, 2005 5:21 pm
by John Cartwright
set your ID to primary key and auto_increment.. that will guarantee the ID will be unique every time.

Posted: Wed Nov 23, 2005 5:46 pm
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???

Posted: Wed Nov 23, 2005 6:50 pm
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

Posted: Wed Nov 23, 2005 7:48 pm
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

Posted: Thu Nov 24, 2005 3:26 am
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.

Posted: Thu Nov 24, 2005 3:44 am
by Grim...
Fetch the latest autonumber id from the database, add 1, then

Code: Select all

$big_id = "Q-".$id."/".$month."/".$year

Posted: Thu Nov 24, 2005 4:41 am
by Jenk
That's poor data control. (For a number of reasons, one that you are experiencing now..)

Seperate the ID and the date.

Posted: Thu Nov 24, 2005 5:50 am
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.

Posted: Thu Nov 24, 2005 9:03 pm
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.

Posted: Fri Nov 25, 2005 2:38 am
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