Re: Re: using rand()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, April 8, 2005 7:41 am, kyriacos sakkas said:
> first I would suggest you get the largest value from the auto increment
> field (maybe use last_insert_id()). Then use rand(1,$cno_max) three
> times to get three random cno numbers, then "select ... from ... where
> cno=$val1 or cno=$val2 or cno=$val3"  should return the three values.
> rand() can also be directly in the sql statement.
> Also manual suggests using mt_rand instead of rand for better performance.

Reasons NOT to do this:

If you ever delete a record, then $val2 might not *BE* there.

To get the max(id) requires an extra query.

MySQL does *NOT* promise the numbers will be 1, 2, 3, 4, ...
Sure, they happen to be that *NOW* and it's unlikely to change, but it's
*NOT* a documented feature.

What if $val1 and $val2 both happen to come out as "42" one day?  Then
you're not going to get 3 different records.  ORDER BY rand() will always
return 3 different records.

It's unlikely that PHP's mt_rand() will be enough faster to justify using
it when a single SQL statement is so much more clear than 5 lines of PHP.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux