Id create in php

Reply

Join Date: Sep 2008
Posts: 138
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Id create in php

 
0
  #1
Jan 9th, 2009
Hi I am working in a shopping database with php. For this I need to create a Id ( automatically) for each product when I add a new item in to the database and I am using php.
I want to create that id automatically and without repetition. It can be 5-10 letters length.

Please help me

Rajeesh
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: Id create in php

 
0
  #2
Jan 9th, 2009
Can you not set the field to auto-increment in the database?
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 148
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Id create in php

 
0
  #3
Jan 9th, 2009
Assuming you cant or dont want to use an auto-incrementing value, I would suggest maybe using a UUID if length is not a concern. PHP lacks direct support, but they can be generated automatically via mysql. http://dev.mysql.com/doc/refman/5.0/...#function_uuid

Although personally i think they are atrocious to look at and have to work with.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 958
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 126
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: Id create in php

 
0
  #4
Jan 9th, 2009
I would really discourage this - use an autoincrement integer (or mediumint - depending on the max no. of products that you envisage your database holding). I image that you are using a database model with primary/foreign key relationships (indexing). Text indexing is slooooooow. If your proposed text primary key is randomly generated, it would have no bearing on the identification of the product (ie no relationship to the name of the product), so I can't see the rationale behind this. However, I'm no major expert.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 138
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Re: Id create in php

 
0
  #5
Jan 9th, 2009
hi thanks for support .. Actually I just want a value like this "54856xcvc" Please help me
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 138
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Re: Id create in php

 
0
  #6
Jan 9th, 2009
And I had a code like this...
  1. $string = md5(microtime() * mktime());
  2. $id = substr($string,0,6);

Will it cause repetition in any condition ????
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Id create in php

 
0
  #7
Jan 10th, 2009
If you were using the full MD5 hash then there would be a small chance for repition, however since you are only using the first 6 characters, this chance is greater.

You must the uniqe product ID be in the format you suggest? Would it not make more sense to have an 'id' column which is an auto-increment and also have a 'product_id' where you can put your fancy ID without fear of having duplicates.

If you really wanted to you could then index the product_id column and seacrh it, but this would not be the best idea as the IDs may not be unique..
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 232
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training

Re: Id create in php

 
0
  #8
Jan 11th, 2009
Try using time(), it produces a number value that is different every second and would never repeat. You could also add a rand() command to add a random number on the end so even if two people created a product at exactly the same time there would be a slim chance that it would be the same;
  1. $ID = time() . rand(0,9); //Unix Time & A random number between 0 and 9
if you wanted to be almost certain not to create any duplicates you could increase the second number in the rand() command

Hope this helps,
Regards,
Sam Rudge
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 138
Reputation: rajeesh_rsn is an unknown quantity at this point 
Solved Threads: 0
rajeesh_rsn rajeesh_rsn is offline Offline
Junior Poster

Re: Id create in php

 
0
  #9
Jan 12th, 2009
ya !!! Thanks very much ....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC