$my_conn = new PDO('sqlite:my_student.sqlite3');
We will use $my_conn to execute our query. <?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);
$sql="SELECT * FROM student WHERE name like '%John'";
echo "<table>";
foreach ($my_conn->query($sql) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
$my_conn = null;
?>
Output ( name column ending with John )
6 Alex John
10 Big John
21 Babby John
name column starting with Bi.
$sql="SELECT * FROM student WHERE name like 'Bi%'";
echo "<table>";
foreach ($my_conn->query($sql) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
Output
10 Big John
14 Bigy
27 Big Nose
32 Binn Rott
name column having 'Ro' at any position
$sql="SELECT * FROM student WHERE name like '%Ro%'";
echo "<table>";
foreach ($my_conn->query($sql) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
Output
7 My John Rob
11 Ronald
15 Tade Row
23 Herod
28 Rojj Base
32 Binn Rott
35 Rows Noump
$val1='%ig%';
$val2='%Rot%';
$sql="SELECT * FROM student WHERE name like :val1 or name like :val2";
$step = $my_conn->prepare($sql);
$step->bindParam(':val1', $val1,PDO::PARAM_STR,10);
$step->bindParam(':val2', $val2,PDO::PARAM_STR,10);
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
Output
10 Big John
14 Bigy
27 Big Nose
32 Binn Rott
Here is a summary of string matching using Like with %
'%John' | Matches string ending with John |
'John%' | Matches string starting with John |
'%John%' | Matches string anywhere with John |
'j%n' | Matches string starting with j and ending with n |
$val1='%Jo%';
$val2='%Ro%';
$sql="SELECT * FROM student WHERE name like :val1 and name like :val2";
Output
7 My John Rob
Using NOT
$sql="SELECT * FROM student WHERE name like :val1 and name NOT like :val2";
Output
1 John Deo
5 John Mike
6 Alex John
10 Big John
21 Babby John
$val1='fe____';
$sql="SELECT * FROM student WHERE sex like :val1";
$step = $my_conn->prepare($sql);
$step->bindParam(':val1', $val1,PDO::PARAM_STR,10);
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo"<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[sex]</td></tr>";
}
echo "</table>";
Using mark column $val1='8_';
$sql="SELECT * FROM student WHERE mark like :val1";
$step = $my_conn->prepare($sql);
$step->bindParam(':val1', $val1,PDO::PARAM_STR,10);
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo"<tr ><td>$row[id]</td><td>$row[name]</td><td>$row[mark]</td></tr>";
}
echo "</table>";
Example : we want to collect all the account numbers ending with 074 in a five digit account number field. Here is the query for this.
SELECT * FROM account_master WHERE acc_no LIKE '__074'
$my_conn->query('PRAGMA case_sensitive_like = ON');
or set the value to OFF to make the search case insensitive.
$val1='%Ro%';
$sql="SELECT * FROM student WHERE name like :val1";
// Case sensitive search by setting next line to ON
$my_conn->query('PRAGMA case_sensitive_like = ON');
$step = $my_conn->prepare($sql);
$step->bindParam(':val1', $val1,PDO::PARAM_STR,10);
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
Download sample script for SQLite with instructions on how to use.Author
🎥 Join me live on YouTubePassionate 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.