DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Id create in php (http://www.daniweb.com/forums/thread167256.html)

rajeesh_rsn Jan 9th, 2009 12:12 pm
Id create in php
 
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

buddylee17 Jan 9th, 2009 4:13 pm
Re: Id create in php
 
Can you not set the field to auto-increment in the database?

mschroeder Jan 9th, 2009 4:45 pm
Re: Id create in php
 
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.

ardav Jan 9th, 2009 7:35 pm
Re: Id create in php
 
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.

rajeesh_rsn Jan 9th, 2009 10:52 pm
Re: Id create in php
 
hi thanks for support .. Actually I just want a value like this "54856xcvc" Please help me

rajeesh_rsn Jan 9th, 2009 10:55 pm
Re: Id create in php
 
And I had a code like this...
$string = md5(microtime() * mktime());
$id = substr($string,0,6);

Will it cause repetition in any condition ????

Will Gresham Jan 10th, 2009 12:58 pm
Re: Id create in php
 
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..

samarudge Jan 11th, 2009 11:18 am
Re: Id create in php
 
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;
$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

rajeesh_rsn Jan 12th, 2009 11:23 am
Re: Id create in php
 
ya !!! Thanks very much ....


All times are GMT -4. The time now is 11:16 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC