I have created my database connection in connection.php and include it in insert.php. Then extend the class DBConnection class in constructor then create a function getData() where I ran a select query while i run my file i get only connection successful message . I tried all the possible options and search for a solution over stackoverflow and other places but failed.
This is my connection.php file
<?php
class DBConnection
{
private $servername;
private $username;
private $password;
private $dbname;
private $conn;
public function __construct()
{
$this->servername = "localhost";
$this->username = "root";
$this->password = "";
$this->dbname = "pdo_test";
try {
$this->conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "connected successfully";
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
}
}
?>
my insert.php
<?php
include('connection.php');
class insertData extends DBConnection
{
private $conn;
public function __construct()
{
$this->conn = new DBConnection();
}
public function getData()
{
$sql = "SELECT * FROM user";
$stmt = $this->conn->prepare($sql);
$res = $stmt->execute();
print_r($res->fetch());
}
}
$id = new insertData();
echo $id->getData();
?>
Can any one point me my error in code? Thanks in advance
Note : Though there has no connection with it but still for more info i am using Ubuntu 18.04