How to randomly select database rows in MySQLi

How to randomly select database rows in MySQLi
How to randomly select database rows in MySQLi

I am making this tutorial for randomly select database rows in Mysql. This is my number ten article. I am create first a HTML page then write Custom CSS and use additional bootstrap CSS and JS. After create HTML page then converted PHP page then create MySql database.

Four rows in my database. Please make this database carefully and I’m create a video tutorial below how to insert data in Mysql database. Download this code. Download option is below end of the article. Try Now.

Making Database

CREATE TABLE `rand` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
`ph` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `rand` (`id`, `name`, `address`, `ph`) VALUES
(1, 'Jon Tab', 'Malaysia', '963258745'),
(2, 'Sur Bharda', 'Island', '56326587'),
(3, 'Samresh Kumar', 'Kochi', '2458745214'),
(4, 'Duo Falla', 'Muka', '326587452'),
(5, 'Solly Han', 'Tubatu', '4569875214'),
(6, 'Silli Tand', 'Paru', '78563215478');

 

index.php

This PHP page showing our result. This code handling large volume of data to randomly select.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to randomly select database rows in MySQLi</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22bootstrap.min.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
<link rel="stylesheet" href="bootstrap.min.css">  
</head>

<body class="bg">


<div class="area1">

<div align="center">
<strong>
    

<h1>How to randomly select database rows in MySQLi</h1>


  </div>


<div align="center">

<form method="POST" class="form-inline" style="margin-top:10px;">
        
			<input type="text" class="form-control" name="val">
            
			<input type="submit" name="more" value="View Query" class="btn btn-primary">
			<input type="submit" name="data" value="Show All" class="btn btn-success">
		</form>


        </div>


</div>


<div class="container" style="margin-top:20px;">

<table border="1" class="table table-bordered table-striped">
  

<tr>
    

<td width="5%">Sl No</td>


<td width="24%">Name</td>


<td width="24%">Address</td>


<td width="47%">Phone No</td>


  </tr>


  <?php $conn = mysqli_connect("localhost","root","password","database nmae"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $find=""; if(isset($_POST['more'])){ $num=$_POST['val']; $find='limit '.$num; } if(isset($_POST['data'])){ $find=''; } $query=mysqli_query($conn,"select * from rand order by id asc $find"); while($row = $query->fetch_assoc()) {
					
						?>
                         

<tr>
							

<td><?php echo $row['id']; ?></td>


<td><?php echo $row['name']; ?></td>


<td><?php echo $row['address']; ?></td>


<td><?php echo $row['ph']; ?></td>


						</tr>


						<?php } ?>
</table>


</div>


</body>
</html>


style.css

*{ padding:0; margin:0;}

.bg{ background:#F4F2F2;}

.area1{ padding-top:30px; }
.input_box{ background:#CCCCCC; width:200px; height:20px; border: thin;  }

 

If you have any comments or questions, feel free to write it below or message me and download our code. Please see video How to make database.

Download Now