PHP SQLite MAX 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.

By using MAX query we will display highest mark.

Highest mark across all classes from student table
// Create (connect to) SQLite database in file
$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$sql="SELECT max(mark) as m_mark FROM student ";

$row = $result->fetch(PDO::FETCH_OBJ);
echo " Highest Mark : ".$row->m_mark;
Output is here
Highest Mark : 96
We can get the details of the student who got maximum mark. Here we will use sub query to get the matching record.
$sql="SELECT * FROM `student` WHERE mark=(select max(mark) from student)";
$row = $result->fetch(PDO::FETCH_OBJ);
echo " Highest Mark : ".$row->mark;
echo " <br>ID  : ".$row->id;
echo " <br>Name  : ".$row->name;
echo " <br>Class  : ".$row->class;
echo " <br>Sex  : ".$row->sex;
Output is here
Highest Mark : 96
ID : 33
Name : Kenn Rein
Class : Six
Sex : female
We can get the highest mark of all classes by using group by query.
$sql="SELECT class,max(mark) as m_mark FROM student group by class";

$step = $my_conn->prepare($sql);
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[class]</td><td>$row[m_mark]</td></tr>";
echo "</table>";
Eight	79
Five	85
Four	88
Nine	65
Seven	90
Six	96
Three	85
Download sample script for SQLite with instructions on how to use.

Check PHP pdo support for SQLite using phpinfo()

PHP SQLite PDO Functions
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

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