Re: Randomly select 5 records from a db

From: Date: Tue, 03 Oct 2000 00:16:26 +0000
Subject: Re: Randomly select 5 records from a db
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-18330@lists.php.net to get a copy of this message
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? :) Thanks, -Ben
<? // 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


« previous php.general (#18330) next »