How to fetch random data from Table in php MySqli


In this example we fetch radom question for online test.For this we need to file

We using rand() function for fetch radom question.

database.php

<?php
$host='localhost';
$username='root';
$password='';
$conn=mysqli_connect($host,$username,$password,"crud");
if(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

retrieve.php

<?php
include_once 'database.php';

$result = mysqli_query($conn,"SELECT * FROM questions order by RAND() limit 5");

?>
<!DOCTYPE html>
<html>
<head>
<title> Retrive data</title>
</head>
<body>
<table>
<tr>
<td>Sl No.</td>
<td>Questions</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr >
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["questions"]; ?></td>
</tr>
<?php
$i++;
}
?> </table> </body> </html>

Related Topic

Fetch data show in select box

Fetch data show in checkbox

Fetch data show in radio button

Fetch data show in text box