$rand_number  = rand(1,$count_number);
   select * from table limit $rand_number,1

解决方案 »

  1.   

    先取出来存为数组$images[],
    接着 srand((double) microtime() * 100000)
    $image_list = array_rand($images, 5);仅供参考而已
      

  2.   

    mistjin(小红点点不见了,看这里看这里) 的做法可取。
    你可以用file_exists()来校验附件是否存在,如果你的附件表里存的是路径的话。
      

  3.   

    谢谢!我只是在想这样全部查询出来会不会比较慢,另外我看到有用
    select * from table_name order by rand() limit 5
    不知道这样如何?
      

  4.   

    $rand_number  = rand(1,$count_number);
    select * from table limit $rand_number,5
    忘记说明了 
    $count_number  是统计出数据库中最近后一条记录的 ID
      

  5.   

    chinaworker(网络混混 Hrcn.CN),
    这样的话如果得到$rand_number正好是对后一个ID呢,那么怎么可能得到5条记录呀?
      

  6.   

    select max(id) as maxid from table
    id是自增编号
    select * from table limit $rand_number,5
    5就是取 5 条数据
      

  7.   

    我试了,这个存在两个问题:
    1、如下:
    mysql> select attachmentid from vbb_attachment;
    +--------------+
    | attachmentid |
    +--------------+
    |           19 |
    |           20 |
    |           21 |
    |           22 |
    |           24 |
    |           25 |
    |           48 |
    |           49 |
    |           50 |
    |           51 |
    |           52 |
    |           53 |
    +--------------+
    12 rows in set (0.05 sec)
    mysql> select max(attachmentid) from vb_attachment;
    +-------------------+
    | max(attachmentid) |
    +-------------------+
    |                53 |
    +-------------------+
    1 row in set (0.00 sec)
    因此,这样:
    mysql> select attachmentid from vb_attachment limit 53,5;
    Empty set (0.00 sec)
    所以也就是说,由于我的ID不是连续的,由于附件可能有删除的,所以ID将会出现不连续,比如:1,3,14,34等
    2、如下:
    总共有12个记录,那么:
    mysql> select attachmentid from vb_attachment limit 10,5;
    +--------------+
    | attachmentid |
    +--------------+
    |           52 |
    |           53 |
    +--------------+
    2 rows in set (0.00 sec)
    这样的话,也就是说如果虽然指定要5个记录,但是由于$rand_number比较靠后,可能取不了5个记录。谢谢!
      

  8.   

    那还不简单$rand_number  = rand(1,$count_number-5);