Finding 10 random records from mySQL table?

Discussion in 'PHP' started by mokimofiki, May 19, 2009.

  1. #1
    I am searching for records that will have hundreds of results but would like the output to just be say 15 randomly not all 800.

    $result = mysql_query("SELECT playername FROM playerstats WHERE level = 37");
    $row = mysql_fetch_array($result);

    The code above will output over 800 results but I want each time the page is loaded it to just randomly pick 15 of the records and display them.

    Any help would be great thank you
     
    mokimofiki, May 19, 2009 IP
  2. bluebenz

    bluebenz Well-Known Member

    Messages:
    876
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    138
    #2
    just an idea (I never use random) :

    - loop 15 times
    - each time, get the record id from that 800 records

    maybe another good solution below me:
     
    bluebenz, May 19, 2009 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    
    $result = mysql_query("SELECT playername FROM playerstats WHERE level = 37 ORDER BY RAND() LIMIT 15");
    
    PHP:
    Regards :)
     
    koko5, May 19, 2009 IP