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
Always use parameterized query when the data is coming from unknown sources. This is required to prevent injection attack.
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