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

&lt;?php
include 'dbconnect.php';
//get the first 10 rows from employee table using MySQLi
   $sql4 = "SELECT * from employee where id&lt;=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:-

&lt;?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&lt;=17 ORDER BY id ASC limit 10";
   $result1 = mysqli_query($con,$sql4);
  
?>
&lt;!DOCTYPE html>
&lt;html>
&lt;head>
&lt;style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
&lt;/style>
&lt;/head>
&lt;body>

&lt;h2>Get first 10 rows from Employee Table&lt;/h2>

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

&lt;/body>
&lt;/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

THOR311 RTP

RATU311

RATU311

THOR311

parlay

CERI188

HOKI311

RATU311

RATU311

judi bola online

Mix Parlay

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

HOKI311 Link Alternatif

CERI188 Link RTP LIVE

THOR311

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

THOR311

HOKI311 Link Apk Resmi

RATU311

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

RATU311

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

THOR311

Ceri188 Game Online

CERI188

link login CERI188

THOR311

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311