Re: Randomly select 5 records from a db
| From: | Paul DuBois | Date: | Tue, 03 Oct 2000 00:21:30 +0000 |
| Subject: | Re: Randomly select 5 records from a db | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18331@lists.php.net to get a copy of this message | ||
At 7:16 PM -0500 10-02-2000, Ben Weinberger wrote:
I had to use the new query-- and it worked to print out results, but it doesn't pull them randomly... it pulls the first 5 in the table and that's it-- and it prints the same 5 each time.... any more ideas? :)Yes, 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.
Thanks, -Ben-- Paul DuBois, paul@snake.net<? // connect to database here $sql = "SELECT * FROM meminfo WHERE type='bene' ORDER BY RAND()"; $result= mysql_query($sql); while($myrow=mysql_fetch_array($result)) { print "$myrow[title]<BR>"; } mysql_close($db); ?> It says "Warning: Supplied argument is not a valid MySQL result resource... on line 9" Line 9 is "while($myrow=mysql_fetch_array($result)) {>while($myrow=mysql_fetch_array($result)) {". Any ideas?Put this after the mysql_query() call. Perhaps you have MySQL 3.22, which requires a different method. if (!$result)print (mysql_error());If that prints an error, try this query instead, where 'x' is some numeric column in your table: SELECT *, x*0+RAND() AS rand_col FROM meminfo WHERE type='bene' ORDER BY rand_col LIMIT 5