We will create the database and connection object.
<?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);
// Query to collect all columns of a particular record
$query="select * from student where id=3";
if($result=$my_conn->query($query)){
$row = $result->fetch(PDO::FETCH_OBJ);
//print_r($row);
echo "ID = $row->id";
echo "<br>Name = $row->name";
echo "<br>Class = $row->class";
echo "<br>Mark = $row->mark";
}
else{
echo " Not able get data ";
print_r($my_conn->errorInfo());
}
$my_conn = null;
?>
In above code we have used student ID to get all columns of the row and displayed the same. We may have to collect the ID value from external source so it is better to use parameter query so we can Binds a parameter to a statement variable.
<?php
require "menu.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);
$id=3; // Student id to collect single record
//Query to collect all column of a particular record
$query="select * from student where id=:id";
$count = $my_conn->prepare($query);
$count->bindParam(':id', $id,PDO::PARAM_INT,5);
if($count->execute()){
$row = $count->fetch(PDO::FETCH_OBJ);
//print_r($row);
echo "ID = $row->id";
echo "<br>Name = $row->name";
echo "<br>Class = $row->class";
echo "<br>Mark = $row->mark";
}
else{
echo " Not able get data ";
print_r($my_conn->errorInfo());
}
$my_conn = null;
?>