Random image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gld4x4
Forum Newbie
Posts: 8
Joined: Wed Feb 18, 2004 6:31 am

Random image

Post 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";
}
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
Post Reply