Re: Randomly select 5 records from a db
| From: | Ben Weinberger | Date: | Tue, 03 Oct 2000 00:32:55 +0000 |
| Subject: | Re: Randomly select 5 records from a db | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18334@lists.php.net to get a copy of this message | ||
Hmm... tried that and still get the same error... odd-- i looked and we are running 3.22.27, and rand() may not work with our version... so-- I've come up with a different solution for now... Here's what I have...
<?
// connect to database here
srand(time());
while(++$count <= 3) {
$num[$count] = rand(0, 8);
$mySQL = "SELECT * FROM meminfo WHERE (type='bene') AND (id='$num[$count]')";
$results = mysql_query($mySQL);
$myrow=mysql_fetch_row($results);
print($myrow[2] . "\n");} mysql_close($db); ?> At 07:27 PM 10/2/2000 -0500, Paul DuBois wrote:
At 7:22 PM -0500 10-02-2000, Ben Weinberger wrote:MySQL is optimizing 3*0+RAND() as a constant expression and evaluating it only once. This is a bug. That's why I said you should use 'x', where 'x' is some numeric column in your table. Actually, it might work with any column. But you need to say x*0+RAND(), not 3*0+RAND(). -- Paul DuBois, paul@snake.netYes, you're doing something wrong. :-) Let's see the exact query. Copy and paste it here from your script. Maybe you forgot the order by clause.Here it is... <? // connect to database here $sql = "SELECT *, 3*0+RAND() AS rand_col FROM meminfo WHERE type='bene' ORDER BY rand_col LIMIT 2"; $result= mysql_query($sql); while($myrow=mysql_fetch_array($result)) { print "$myrow[title]<BR>"; } mysql_close($db); ?>