Re[2]: [PHP] random rows using MySQL's rand()
| From: | Tom Rogers | Date: | Tue, 22 Oct 2002 02:55:55 +0000 |
| Subject: | Re[2]: [PHP] random rows using MySQL's rand() | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-120848@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, October 22, 2002, 12:53:10 PM, you wrote:
TR> Hi,
TR> Tuesday, October 22, 2002, 10:54:05 AM, you wrote:
T>> Hi all,
T>> I was trying to use the following query to randomly retrieve a row from
T>> MySQL table.
T>> $query = "SELECT * FROM my_table ORDER BY RAND() LIMIT 1";
T>> This query works fine in MySQL client, access via command-line.
T>> But when using this via PHP, it doesn't retrieve the row randomly for a
T>> small set of records, ie. 3.
T>> Now that I've increased the number of records to 32, it starts to show
T>> random behaviour.
T>> So, just wondering whether this is something I can expect for a table with
T>> only a few records.
T>> Regards,
T>> Tanat
TR> For small numbers you would probably be better off generating the
TR> random number in php from 1 to numrows and use that in the select like:
TR> LIMIT $rand,1
TR> --
TR> regards,
TR> Tom
That probably should have been LIMIT $rand-1,1 as limit is 0 based :)
--
regards,
Tom