Page 1 of 1

Random image

Posted: Thu Feb 19, 2004 10:35 am
by gld4x4
I have a script that selects random images, the problem is it keeps picking the first record in the database and the other 2 are random. Can anybody please help. Below is the code used.

<?
$db = MYSQL_CONNECT("localhost",rrrrr,rrrrrr) OR DIE ("Unable to connect to database");
@mysql_select_db(attorney_images,$db) or die ("Unable to select database");
$result = mysql_query ("SELECT *FROM all_images order by RAND ()LIMIT 3");
if($myrow = mysql_fetch_array($result))
{
do{
echo"<td><table width='100%'><tr><td align='center'><table border='1'><tr><td><img src=http://rrrrrr.com/images/pics/$myrow[0]
</td></tr></table></td></tr><tr><td align='center'><b><font color='000000' size='-2'>$myrow[2]<br>$myrow[4]</font><br>$myrow[3]</b></font></td></tr></table>";
}while ($myrow = mysql_fetch_array($result));
}else{
echo "bad image";
}
?>

Posted: Thu Feb 19, 2004 10:41 am
by markl999
You are doing 2 mysql_fetch_array()'s.
You don't need the do{ ... }while loop, just
while($myrow = mysql_fetch_array($result)){
...
}
if(you want to check if the image exists or not you could just use http://php.net/file_exists inside the while loop.