我需要从6个随机记录中提取数据,但我遇到了一些麻烦[重复]

Possible Duplicate:
Doing a while / loop to get 10 random results

I'm making a website which people can upload funny pictures they take or make. Each time the home page is loaded, a random picture/record is displayed from the MySQL database onto the page. This is done with...

$filename = mysql_fetch_row(mysql_query("SELECT filename FROM pictures ORDER BY RAND() LIMIT 1"));
echo $filename[0];

This would echo a random pictures file name onto the page. Something like funny_cat. But then I need the file type so that I can echo out the image onto the page. Like this...

while ($record = mysql_fetch_array(mysql_query("SELECT * FROM pictures WHERE p='$p'"))) {
    $filetype = $record["filetype"];
}
echo "<img src=\"picture/".$filename[0].".".$filetype."\" />";

All is good.

Now here's the bit I'm struggling on. I'm wanting to display 6 other random smaller pictures onto the page. So I need to get 6 random records and echo the filename and filetype for each record. The thing that needs considering is I can only get random records with the technique I used above due to the way the database is set up. How could I do this?

If there is anything you're unsure about please say and I'll try explain. Also, please explain your solutions. Thanks!! :-)

Retrieve the first 6 results of the randomized list by changing your query's limit to 6.

$result = mysql_query("SELECT filename FROM pictures ORDER BY RAND() LIMIT 6")

Then, you can call mysql_fetch_row 6 times to get all 6 file names.

$filenames = array();
while ($row = mysql_fetch_row($result)) {
    $filesnames[] = $row[0];
}

Now, you have all 6 file names, and you can do image retrieval for each.

If I understood you correctly you wanted to "replicate" the random fetch 6 times. If so I'd do something like so:

for($i = 0; $i < 6; $i++){
   $filename = mysql_fetch_row(mysql_query("SELECT filename,filetype FROM pictures ORDER BY RAND() LIMIT 1"));
   $file =  $filename[0].'.'.$filename[1];
   echo "<img src=\"picture/".$file."\" />";
}

Note: If you are fetching a big amount of data, don't use the rand function, but rather create a random array in php and fetch the relative ids.

Edit:

If you wanted a solution on retrieving 6 different and random pictures (so they won't repeat themselves) I would do something like so:

$arr_ids = array();
    for($i = 0;$i < 6;$i++){
    $fresh_rand = rand()*$db_row_count;//$db_row_count the number of rows in the data base
     for($j=0;$j <count($arr_ids);$j++){
      if($fresh_rand == $arr_ids[$j]){
        $j =0;
         $fresh_rand = rand()*$db_row_count;
      }
     }
     $arr_ids[$i] = $fresh_rand;
    }

Then I would fetch from the db, the ids from the $arr_ids array.
My solution was made in a hurry, so forgive me for the bad practice, but I hope you understood what you need to do.