Select First 10 Rows from MySQL using PHP

Hi friends, in this tutorial, you will learn how to select first 10 rows from MySQL using PHP. This kind of query is required very often when you need a certain number of rows from your MySQL database to display in the application or in the browser.

Also read, Delete a row in MySQL

There are various methods in MySQL such as PDO, MySQLi, etc by which we can do this. In order to do this, you can use two approaches of MySQL query as mentioned in the below steps.

Steps to select First 10 Rows from MySQL using PHP

Step 1:- First of all, create an HTML file inside the root directory of your local server.

Step 2:- Set up the database connection to your MySQL database as shown below

<?php
$servername='localhost';
$username="root";
$password="";
$dbname="myproject_db";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}
?>

Step 3:- Create a table in the MySQL database as shown below

DDL information of the table

CREATE TABLE employee (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
age int(20) NOT NULL,
department varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 4:- You must have some data in the table as shown below so that you can display those data with the help of different queries.

Select First 10 Rows from MySQL using PHP

Step 5:- Now, get the first 10 rows from the table by using the below query with LIMIT as shown below

<?php
include 'dbconnect.php';
//get the first 10 rows from employee table using MySQLi
   $sql3 = "SELECT * from employee ORDER BY id ASC limit 10";
   $result = mysqli_query($con,$sql3);
?>

Step 6:- Also, you can get the first 10 rows from the table using the below query with comparison such as column name<= number value

<?php
include 'dbconnect.php';
//get the first 10 rows from employee table using MySQLi
   $sql4 = "SELECT * from employee where id<=17 ORDER BY id ASC limit 10";
   $result1 = mysqli_query($con,$sql4);
?>

From the above query, you have noticed that I have used id<=17 because my 11th row is 17

Step 7:- Now, you can display the data in an HTML table as shown in the below code

Complete Code:-

<?php
include 'dbconnect.php';

   //get the first 10 rows from employee table using MySQLi
   $sql3 = "SELECT * from employee ORDER BY id ASC limit 10";
   $result = mysqli_query($con,$sql3);
   

   $sql4 = "SELECT * from employee where id<=17 ORDER BY id ASC limit 10";
   $result1 = mysqli_query($con,$sql4);
  
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>
</head>
<body>

<h2>Get first 10 rows from Employee Table</h2>

	<table style="width:50%">
		  <tr>
		    <th>Sl No</th>
		    <th>Employee</th> 
		    <th>Age</th>
		    <th>Department</th>
		  </tr>
		<?php
		   if(mysqli_num_rows($result1)>0)
		   {
		   	$inc = 1;
		   	 //output data for each row
		   	  while($row = mysqli_fetch_assoc($result))
		   	  {
		   	  	
		   	  	echo '<tr>
		   	  				<td>'.$inc++.'</td>
						    <td>'.$row['name'].'</td>
						    <td>'.$row['age'].'</td>
						    <td>'.$row['department'].'</td>
						  </tr>';
			   	  }
			   }
				  
		?>
	</table>

</body>
</html>

Also read, Export Data From MySQL to Excel Using PHP

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.

Leave a Comment

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311