FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

PHP Tutorial: Fetching random record from the Database

jawahar
5/2/2005 1:20:53 AM, Views: 1827
The following code illustrates how to fetch random record from the
Database using PHP

There are several tasks where you need to output multiple records
from the database in a random order. For example it can be the
banner rotation system or the "random quote" generator. There are
different programming techniques available and I'll show you here
two most common ways to compelete this task.

This is how I built my script when I first time tried to make a
random line of text appear on the page:

.... // database connection code
$query = "SELECT * FROM QUOTES";
$result = MYSQL_QUERY($query);
$randval = rand(0, mysql_num_rows($result) - 1);

$quotetext = mysql_result($result, $randval, 1);
$quoteauthor = mysql_result($result, $randval, 2);

print "$quotetext<br>$quoteauthor";

Let me explain the code written above. First, we set a simple SQL
query to return all rows from the table QUOTES. Then we choose the
random value not greater than number of rows returned by the
database. In mysql_result() function we use random value as a row
position identifier and the third argument of this function is the
data field position (column).
Note a few disadvantages of this script:
1.) We should manually seed random values generator somewhere in the
code preceding these lines. By the way it should be done with srand
() function.
2.) We should store in mind what data field we need to read, I mean
the last argument in the mysql_result() function. And if the number
of columns in the table changes, we would have to rewrite the code.

Now let me continue and show you another code example that does
exactly the same thing but in more convenient way:

$query = "SELECT * FROM QUOTES ORDER BY RAND() LIMIT 1";
$result = MYSQL_QUERY($query);
$row = mysql_fetch_array($result);
print($row['Text'] . '<br>' . $row['Author']);


In this example MySQL database generates random values for us and it
returns different values each time. With some simple modifications
we can output a set of records in a random order:

$query = "SELECT * FROM QUOTES ORDER BY RAND()";
$result = MYSQL_QUERY($query);
while($row = mysql_fetch_array($result)){
    print($row['Text'] . '<br>' . $row['Author'] . '<hr>');
}
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Avg Visitor Rating: Average Visitor Rating is 5 out of 5
Number of Ratings : 3 Votes
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
PHP RSS Feed
Most Popular Tutorial
Most Popular Solution
No Records!
Top Rated
Top Rankers
Overall
1. jawahar (500)
Yearly -2008
No Rankings!
Expertsforge Sponsors
bnrtop