Home Tutorial Php Sql Php Sql Random

 
 

Share on Google+Share on Google+
Php Sql Random
Posted on: December 10, 2010 at 12:00 AM
Advertisement
This example illustrates how to fetch random data from database table.

Php Sql Random

This example illustrates how to fetch random data from database table.

In this example we create select statement with ORDER BY RAND() clause. The RAND() clause is used to fetch data from table randomly. The output display on the browser  as below, when refresh the browser data fetched randomly from table.

 

Table: users

 

Source Code of sql_random.php 

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con){
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("test", $con);

  $result = mysql_query("SELECT email FROM users ORDER BY RAND()");

  if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
  }

  $row = mysql_fetch_array($result);
  echo "<b>Email: </b>";
  echo $row['email']."<br>";
?>

Download Source Code

 

Output:

 

Advertisement

Related Tags for Php Sql Random:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 10, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com