Re: Random Unique ID
| From: | Németh Zoltán | Date: | Fri, 23 Mar 2007 12:37:18 +0000 |
| Subject: | Re: Random Unique ID | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-251164@lists.php.net to get a copy of this message | ||
2007. 03. 23, péntek keltezéssel 08.13-kor Robert Cummings ezt írta:
> 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.
yes, of course. that's good only for this small test, not for real life
use ;)
for real use one should check mysql_errno end take action based on that
greets
Zoltán Németh
>
> Cheers,
> Rob.