PHP SQLite MIN query

SQLite

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 min query we will display lowest mark.

Lowest mark across all classes from 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 min(mark) as m_mark FROM student ";

$result=$my_conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ);
echo " Lowest Mark : ".$row->m_mark;
?>
Output is here
Lowest Mark : 18
We can get the details of the student who got minimum mark. Here we will use sub query to get the matching record.
$sql="SELECT * FROM `student` WHERE mark=(select min(mark) from student)";
$result=$my_conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ);
echo " Lowest 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
Lowest Mark : 18
ID : 19
Name : Tinny
Class : Nine
Sex : male
We can get the lowest mark of all classes by using group by query.
$my_conn = new PDO('sqlite:my_student.sqlite3');
  // Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE, 
                            PDO::ERRMODE_EXCEPTION);
$sql="SELECT class,min(mark) as m_mark FROM student group by class";

$step = $my_conn->prepare($sql);
	
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[class]</td><td>$row[m_mark]</td></tr>";
}
echo "</table>";	
	
 $my_conn = null;
Output
Eight	79
Five	75
Four	55
Nine	18
Seven	55
Six	54
Three	55
Download sample script for SQLite with instructions on how to use.

Check PHP pdo support for SQLite using phpinfo()

PHP SQLite PDO Functions


plus2net.com







Post your comments , suggestion , error , requirements etc here




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