Re: Random Unique ID

From: Date: Fri, 23 Mar 2007 12:13:54 +0000
Subject: Re: Random Unique ID
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-251163@lists.php.net to get a copy of this message
On Fri, 2007-03-23 at 09:15 +0100, Németh Zoltán wrote: > // method 2 > $time = microtime(TRUE); > for ($i = 1; $i <= 5000; $i++) { > $done = FALSE; > while (!$done) { > $id = md5((microtime(TRUE) * (rand(1,1000) / 100))); > $sql = "INSERT INTO idtest (id,cnt) VALUES ('$id', $i)"; > $result = mysql_query($sql); > if ($result) { $done = TRUE; } > } > } > $end = microtime(TRUE) - $time; > echo "method 2 time: " . $end . "<br>"; One of the problems with the above style is that if the query fails for some other reason (typo, no permissions, database temporarily unavailable, etc) the code will run indefinitely (or until it gets killed for too much time). That's a very dirty way to manage failed inserts due to dupes. Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#251163) next »