Help with creating unique ID in PHP
Moderator: General Moderators
Help with creating unique ID in PHP
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...
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...
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Is this mysql?
itd be a matter of
to get the last insert id for the current month
itd be a matter of
Code: Select all
SELECT `id` FROM `table` WHERE `month` = $current_month ORDER BY `id` DESC LIMIT 1Code: 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;-NSF
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.
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.
Fetch the latest autonumber id from the database, add 1, then
Code: Select all
$big_id = "Q-".$id."/".$month."/".$yearNo 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.Jcart wrote:set your ID to primary key and auto_increment.. that will guarantee the ID will be unique every time.