douhui9192 2012-06-11 17:14
浏览 84

我需要从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!! :-)

  • 写回答

2条回答 默认 最新

  • duanbipu7601 2012-06-11 17:20
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)