I'm sure a better programmer would come up with a more elegant solution, but I've been using this bit of php for a while to get random photos from specified directories and display thumbs on a WordPress blog.
`<?php`
`$link2ID = mysql_connect("database.vista.howlingpoint.net", "userid", "password");`
`mysql_select_db("database", $link2ID);`
`$result2ID = mysql_query("SELECT albumid, filename, title,`desc`FROM zp_images WHERE (albumid = '3' OR albumid = '21') ORDER BY RAND() LIMIT 1", $link2ID);`
`while (list($albumid, $filename, $title2, $zen_caption) =` `mysql_fetch_array($result2ID))`
`{`
`$result3ID = mysql_query("SELECT folder FROM zp_albums WHERE id='$albumid'", $link2ID);`
`list($folder) = mysql_fetch_array($result3ID);`
`$folder2 = urlencode($folder);`
`print "<a href=\"http://vista.howlingpoint.net/z/$folder/$filename.php\" title=\"$title2: $zen_caption\" target=\"_blank\"><img src=\"http://vista.howlingpoint.net/z/zp-core/i.php?a=$folder2&i=$filename&w=140&q=80\" alt=\"$title2: $zen_caption\" border=\"1\"></a>";`
`}`
`mysql_close($link2ID);`
`?>`
Examples in action are howlingpoint.net/life and beachlaw.info. Note for my server the query for the desc element had to be in backticks to avoid an error.