Am trying to access the result of SQL query in SQLite using PHP from another class. I have read similar questions here and here. But still have not found a solution to my problem. This is what I have done.
DBMan.php
<?php
Class DBMan
{
private $dsn = 'sqlite:leDB.db';
private $db;
public function __construct()
{
$this->db = new PDO($this->dsn);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function showData()
{
try{
$sql = 'SELECT * FROM fruits';
$stmt= $this->db->query($sql); //UPDATED
if(!$stmt){
throw new PDOException('Error displaying fruits');
}
$data = $stmt->fetchAll(PDO::FETCH_ASSOC); //UPDATED
return $data;
}
catch (PDOException $e){
echo 'Error\n';
echo $e->getMessage(); //UPDATED
}
}
}
?>
MaView.php
<?php
class MaView
{
include_once("DBMan.php"); //UPDATED
$db = new DBMan();
$val = $db->showData();
foreach ($val as $row) {
echo "<H1>" . $row['fruit_name'] . "</H1>"; //UPDATED
}
}
?>
Can someone please show me where I have blundered?