Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unique id

0 views
Skip to first unread message

roger

unread,
May 8, 2005, 12:49:07 PM5/8/05
to
Using the epoc timestamp can someone tell me how to create a unique id for a
product so that if two users ad a product at the same time they can be
assigned different id's IE..

PSuedo code

Get unix timestamp ie 1115501044
While(ID does exist in DB)
{
Increment ID by 1
}
ID = newvalue;
but how would i do this in php..

Thanks in advance


Oli Filth

unread,
May 8, 2005, 12:55:09 PM5/8/05
to

Any database system (e.g. MySQL) will automatically do this for you if
you select a field as AUTOINCREMENT.


--
Oli

roger

unread,
May 8, 2005, 1:23:56 PM5/8/05
to
It cant be set to AutoIncement as it corresponds to the time the product was
updated..
i have managed to do it using:

$testid='1115571604';
$unique_result='1';
while($unique_result)
{
$rs= mysql_query("Select ID from Items where ID =
'$testid'");
if (mysql_fetch_array($rs))
{
$testid++;
}
else $unique_result = '0';
}
echo "$testid";

but how can i lock it so no one straight after can grab the same value
during the time it takes to execute the rest of the code following this.

"Oli Filth" <ca...@olifilth.co.uk> wrote in message
news:Nvrfe.11047$996....@newsfe6-gui.ntli.net...

Oli Filth

unread,
May 8, 2005, 2:04:20 PM5/8/05
to
roger wrote:
> It cant be set to AutoIncement as it corresponds to the time the product was
> updated..
> i have managed to do it using:
>
> $testid='1115571604';
> $unique_result='1';
> while($unique_result)
> {
> $rs= mysql_query("Select ID from Items where ID =
> '$testid'");
> if (mysql_fetch_array($rs))
> {
> $testid++;
> }
> else $unique_result = '0';
> }
> echo "$testid";
>
> but how can i lock it so no one straight after can grab the same value
> during the time it takes to execute the rest of the code following this.

Why do you need it to correspond *uniquely* to the creation time? Like I
said previously, if you need a unique ID, then use the autoincrement
feature. If you need a timestamp, then store the timestamp as well. It's
considered bad design practice to have one database field serving two
purposes, not least because it involves kludges like this.

What's more, by changing the ID field of records on UPDATEs, you're
destroying the relational aspect of the database, because you can't use
this ID value as a foreign key in other tables (i.e. you can't identify
this row from other tables).

But if you still want to do this, then use a table lock, i.e.

LOCK TABLE table_name WRITE;

...

UNLOCK TABLES;


P.S. It would be much more efficient if you treat the ID value in your
example above as an integer rather than a string.

--
Oli

roger

unread,
May 8, 2005, 6:01:23 PM5/8/05
to
Its just the way the site is designed, i dont have any say on it. I just
have to write an app to work with the existing site..

"Oli Filth" <ca...@olifilth.co.uk> wrote in message

news:Ewsfe.22261$YF5....@newsfe5-win.ntli.net...

John Sjouken

unread,
May 9, 2005, 2:21:17 PM5/9/05
to
The only things you can do is ask them if they are willing to make the row
"UIQUE".
An other thing is using a multification with timestamp and one of the inputs
which will change the mast, use MD5-hash with it multiply it and use it as
an unique field.
The only way to update this field is using the same time-stamp AND the same
input
--
John, PE1DSR

http://ham.jen-gen.com
"roger" <pag...@ntlworld.com> wrote in message
news:T_vfe.26191$5A3....@newsfe4-win.ntli.net...

roger

unread,
May 10, 2005, 2:27:55 AM5/10/05
to
ok thanks
"John Sjouken" <sjo...@iae.nl> wrote in message
news:427faa1d$1...@nnrp1.psineteurope.com...
0 new messages