PHP SQLite LIMIT Query

We will create the database and connection object.
$my_conn = new PDO('sqlite:my_student.sqlite3');
We will use $my_conn to execute our query.

Read more on limit query here.

USING limit query

LIMIT query takes two inputs, one is the starting position of the record and another is number of records. We will add these two values along with SELECT query using LIMIT.
<?php 
 // Create (connect to) SQLite database in file
$my_conn = new PDO('sqlite:my_student.sqlite3');
  // Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE, 
                            PDO::ERRMODE_EXCEPTION);
////////////////////	
echo "<br>LIMIT 0, 10 <br><br>";
$sql="SELECT * FROM student  LIMIT 0,10";

$step = $my_conn->prepare($sql);
	
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td></tr>";
}
echo "</table>";
?>
Output
1	John Deo	Four
2	Max Ruin	Three
3	Arnold	Three
4	Krish Star	Four
5	John Mike	Four
6	Alex John	Four
7	My John Rob	Five
8	Asruid	Five
9	Tes Qry	Six
10	Big John	Four
Note that the first record is at 0th position and 10 records are returned staring from 0th position record.

Using Parameters

$val1=5;
$val2=10;
echo "<br>LIMIT 5, 10 <br><br>";
$sql="SELECT * FROM student  LIMIT :val1,:val2";

$step = $my_conn->prepare($sql);
	$step->bindParam(':val1', $val1,PDO::PARAM_INT,10);
	$step->bindParam(':val2', $val2,PDO::PARAM_INT,10);
	$step->execute();
	$step = $step->fetchAll();
	echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td></tr>";
}
echo "</table>";	
Output is here

Output
LIMIT 5, 10

6	Alex John	Four
7	My John Rob	Five
8	Asruid	Five
9	Tes Qry	Six
10	Big John	Four
11	Ronald	Six
12	Recky	Six
13	Kty	Seven
14	Bigy	Seven
15	Tade Row	Four

SQLite Paging

SQLite Paging or records


Podcast on SQLite database management using PHP PDO

Download sample script for SQLite with instructions on how to use.

DISTINCT : Getting unique records

PHP SQLite PDO Functions
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer