You should rather use PHP to randomly shuffle the array. If the field you are interested in is called url
:
$req = mysql_query("SELECT url FROM images WHERE user_id = '{$row['userID']}'");
$images = array();
while($image = mysql_fetch_array($req)) {
$images[] = $image['url'];
}
shuffle($images);
$tenImages = array_slice($images, 0, 10);
Edit. And please consider using PDO for prepared statements.
Update. Why not picking the ten images one at a time?
$images = array();
while(count($images) < 10) {
$req = mysql_query("SELECT url FROM images WHERE user_id = '{$row['userID']}' LIMIT " . rand(1, 10000) . ", 1");
$image = mysql_result($req, 0, 0);
if(!in_array($image)) {
$images[] = $image;
}
}
Funnier (and faster) way. Provided here.
$ids = array();
for($i = 0 ; $i < 1000 ; $i++) {
$ids[] = rand(1, 10000);
}
$req = mysql_query("SELECT url FROM images WHERE user_id = '{$row['userID']}' AND id IN (" . implode(',', $ids) . ") LIMIT 10");