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

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 [URL=“http://php.net/manual/en/function.array-rand.php”]array_rand() on the result set to grab x random rows.

$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.

Thanks for pointed out. I read it now.

Hello there again SpacePhoenix,

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