PHP SQL Random

The PHP SQL Random is used to fetch data randomly from database in PHP. The
Rand ( ) clause is used to fetch the random data from table into the
browser.
Understand with Example
The Tutorial illustrate an example from PHP SQL Random. To understand and
elaborate the example we have a table 'users' that show the records of table
'users'.
Table: users

Source Code of sql_random.php
This example show you to fetch random data from database table. In this example we create select statement with ORDER BY RAND()
clause in PHP. The RAND( ) clause is used to fetch data from table randomly.
<?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>";
?>
|
when we refresh the browser, it randomly fetched the
data from the table and display on the browser.
Output:

Download Source Code

|