SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Is there any way to select random record from database with mysqli

    Hi,

    I'm a newbie to MySQLi and have searched the internet for almost two weeks but cannot find any best result.

    Is there any good way to select random records from database using mysqli better than ORDER BY RAND() function?

    I heard this function takes times to process just to pick out five from a million records in the database.

    Or else, should I use a traditional MySQL way instead of this.

    Thanks in advance

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Have a read of this thread where different methods are discussed. The method I personally prefer is to run the select query without the use of a limit to get all records that can be chosen from then once the result set is in an array. Then I would use array_rand() on the result set to grab x random rows.

    PHP Code:
    $chosen_records=array_rand($all_records,$number_required); 
    array_rand() is a PHP function but hopefully whatever language your using will have an equivalent function.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for pointed out. I read it now.

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hello there again SpacePhoenix,

    I've tested your code. It's perfect for me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •