Jump to content


Pull a Random Row

  • Please log in to reply
1 reply to this topic

#1 yarub

  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 05 March 2006 - 10:39 PM

Let's say I have 100 entries in a database, and I'd like to just put one random entry on my homepage. How would I go about doing that? It doesn' t have to be for a set period of time or anything. I just want to have a random entry with every refresh of the page. Is that easy to do? >_>

#2 earl_dc10

  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 06 March 2006 - 12:19 AM

yes, very easy, just do this:

$select = "SELECT * FROM $table";
$select_query = mysql_query($select, $link)
   or die("Couldn't select <value".mysql_error() );
$num = mysql_num_rows($select_query) // get the number of rows
$id = $num-1; // since mysql_num_rows starts at 1and id's start at 0

$rand = rand(0, $id); //random statement

$get = "SELECT <value> FROM $table WHERE id='$rand'";
$get_query = mysql_query($get, $link)
   or die("Couldn't select <value>".mysql_error() );

got a problem? Google helps many of those in need

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users