i use this:
you need to store the image's filename in the database... if there is so image there is no entry for "picture" in the table "books"
table books;
int id (key),
varchar title,
varchar pic
works for me - though i am sure the rand/array ops can be written better - i need to redo it sometime...
$numRandPics = 1;
$query = "SELECT id from books WHERE pic IS NOT NULL";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$picElements[] = $row[id];
}
for ($i = 0; $i < $numRandPics; $i++) {
$max = count($picElements) - 1;
srand((double)microtime()*1000000);
$randomNum = rand(0,$max);
$queryGet = "SELECT id, title, pic from books WHERE id = '$picElements[$randomNum]'";
$resultGet = mysql_query($queryGet);
if ($rowGet = mysql_fetch_array($resultGet)) {
$picture = "$rowGet[pic]";
$title = stripslashes($rowGet[title]);
print "<A href=\"books.php?id=$rowGet[id]\"><IMG SRC=\"$picture\"><BR>$title</A>";
}
}