That really isn't the best way of doing, an easier way would be:
$sql = "SELECT * FROM mytable";
$qry = mysql_query($sql);
$nums = mysql_num_rows($qry);
$rand = rand(0,$nums);
$sql = "SELECT `filename` FROM mytable WHERE id = $rand";
$qry = mysql_query($sql);
$file_name = mysql_fectch_assoc($qry);
$ret = 'images/myimages' . $file_name['filename'] . '.jpg';
return $ret;
That will give you the randomised file name to put in your image tag. That's providing you have the file names stored in the table.
If you are using PHP5 then you don't need to db entry at all.
$dir = "images/myimages";
$scanned = scandir($dir,1);
$total = count($scanned);
$total = $total - 2; // so you don't get the '.' and '..'
$rand = rand(0,$total);
$ret = $dir . $scanned[$rand] . '.jpg';
return $ret;