php - Randomly get multiple rows from a table -


right i'm selecting 3 random rows table using order rand(); pointed out on web method slow. using on database 30 rows , takes long time return value. of other solutions i've found return 1 row. what's best way return multiple random rows?

$get_projects_query =    mysql_query(     "select p_id project_data p_featured='1' order rand() limit 3"   ) or die(mysql_error());  while($project_row = mysql_fetch_array($get_projects_query)) {?>   //do stuff }  //end 3 random featured projects loop) 

if there 30 rows, have couple other options.

option #1

  1. fetch 30 rows.
  2. call shuffle()
  3. read first 3 off pile

option #2

  1. generate 3 random numbers 0 29. call them r1, r2, r3.
  2. select ... p_id in (r1, r2, r3)

option #3

  1. select p_id ...
  2. while ($row = mysql_fetch_assoc($result)) $idlist[] = $row["p_id"];
  3. shuffle($idlist)
  4. select ... p_id in ($idlist[0], $idlist[1], $idlist[2])

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -