PHP SQLite DISTINCT

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.

By using DISTINCT we can get unique classes from our student table .
<?php 
   $my_conn = new PDO('sqlite:my_student.sqlite3');
  // Set errormode to exceptions
  $my_conn->setAttribute(PDO::ATTR_ERRMODE, 
                            PDO::ERRMODE_EXCEPTION);
  $sql="SELECT DISTINCT(class) as class FROM student ";
  
  echo "<table>";
   foreach ($my_conn->query($sql) as $row) {
	echo "<tr ><td>$row[class]</td></tr>";
   }
  echo "</table>";
?>
Output
Four
Three
Five
Six
Seven
Nine
Eight
Using DISTINCT query we can collect unique classes and populate a drop down list box as options. User can select one of the option from the pull down list box.
$sql="SELECT DISTINCT(class) as class FROM student ";
  
  echo "<SELECT name=class>";
   foreach ($my_conn->query($sql) as $row) {
	echo "<option value='$row[class]'>$row[class]</option>";
   }
  echo "</SELECT>";
Output is here



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

LIMIT : Limiting number of records from starting position

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