PDA

View Full Version : Chosin a row randomly from a mysql table


DjSap
03-01-2002, 12:26 PM
Well im making a hot or not (photo rating) hack for my board, its practily done but i dont know how to solve the randomizing thingy so that when people load hotornot.php then a random image is shown. From reading a tutorial i learned a little and used the code they supplied to write this query:


$result= $DB_site->query("select * from photoalbum WHERE allowrate='1'");
$row=mysql_num_rows($result);
srand((double)microtime()*1000000);
$random=rand(1,$row);
$messages = $DB_site->query("SELECT * FROM photoalbum WHERE picid='$random'");

The problem is that since i have deleted some pics from the table photoalbum then the id field goes like 2,4,5,8 and so on. Now when the random image is shown sometimes an non existing row is chosen since the random thingy may say 3 even though there is no row with the id 3. How do i make it randomize the rows that actually exist?

Admin
03-01-2002, 12:30 PM
SELECT * FROM photoalbum ORDER BY RAND()

DjSap
03-01-2002, 12:37 PM
lol it was that easy, heh well thanx again for helping me out.