<?php
try {
// 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);
// prepare Query
// $sql="select * from student"; // All students
$sql="SELECT * FROM student WHERE class='Four'";
echo "<table>";
foreach ($my_conn->query($sql) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
}
catch(PDOException $e)
{
// Print PDOException message
echo $e->getMessage();
}
$my_conn = null;
?>
try {
$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$class='Four'; // Parameter, value can be changed.
$sql="SELECT * FROM student WHERE class=:class LIMIT 0,3";
$stmt = $my_conn->prepare($sql);
$stmt->bindParam(':class', $class,PDO::PARAM_STR,10);
$stmt->execute();
// Fetch one row at a time
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Class: " . $row['class'] . "<br>";
}
} // end of try
catch(PDOException $e)
{
// Print PDOException message
echo $e->getMessage();
}
$my_conn = null;
Output is here
ID: 1, Name: John Deo, Class: Four
ID: 4, Name: Krish Star, Class: Four
ID: 5, Name: John Mike, Class: Four
$sql = "SELECT * FROM student";
$stmt = $my_conn->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
echo "Name: " . $row[0] . ", Class: " . $row[1] . ", Mark: " . $row[2] . ", Gender: " . $row[3] . "
";
}
$sql = "SELECT * FROM student";
$stmt = $my_conn->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
echo "Name: " . $row['name'] . " / " . $row[0] . ", Class: " . $row['class'] . " / " . $row[1] . "
";
}
$sql = "SELECT * FROM student";
$stmt = $my_conn->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
echo "Name: " . $row->name . ", Class: " . $row->class . ", Mark: " . $row->mark . ", Gender: " . $row->gender . "
";
}
$sql = "SELECT * FROM student";
$stmt = $my_conn->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_LAZY)) {
echo "Name: " . $row->name . " / " . $row['name'] . " / " . $row[0] . ", Class: " . $row->class . " / " . $row['class'] . " / " . $row[1] . "
";
}
<?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);
$class='Four'; // Parameter, value can be changed.
$sql="SELECT * FROM student WHERE class=:class";
$stmt = $my_conn->prepare($sql);
$stmt->bindParam(':class', $class,PDO::PARAM_STR,10);
$stmt->execute();
$stmt = $stmt->fetchAll();
echo "<table>";
foreach ($stmt as $row) {
echo "<tr ><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
$my_conn = null;
?>
Output is here
ID: 1, Name: John Deo, Class: Four
ID: 4, Name: Krish Star, Class: Four
ID: 5, Name: John Mike, Class: Four
Other PDO constants with fetchAll()
$stmt=$stmt->fetchAll(PDO::FETCH_ASSOC); // default
echo "<table>";
foreach ($stmt as $row){
echo "<tr><td>$row[id]</td><td>$row[name]</td></tr>";
}
echo "</table>";
try {
$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$class='Four'; // Parameter, value can be changed.
$sql="SELECT name FROM student WHERE class=:class LIMIT 0,3";
$stmt = $my_conn->prepare($sql);
$stmt->bindParam(':class', $class,PDO::PARAM_STR,10);
$stmt->execute();
while ($name = $stmt->fetchColumn()) {
echo "Student Name: ".$name ."<br>";
}
} // end of try
catch(PDOException $e)
{
// Print PDOException message
echo $e->getMessage();
}
$my_conn = null;
Output
Student Name: John Deo
Student Name: Krish Star
Student Name: John Mike
<?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 DISTINCT(class) as class FROM student";
echo "<input name='my_class' list='class' />
<datalist id='class'>";
foreach ($my_conn->query($sql) as $row) {
echo "<option value=$row[class]>"; // adding options
}
echo "</datalist>";
?>

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.