sayalok
sayalok

Reputation: 920

PHP PDO select query return no result

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

Upvotes: 0

Views: 171

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

Although I think your class hierarchy isn't right, the problem is that in your insertData class, you have a constructor which creates a DBConnection instance and assigns it to $this->conn. So when you refer to $this->conn you are referring to DBConnection instance and not a PDO object. So your call to

$stmt = $this->conn->prepare($sql);

will fail as DBConnection doesn't have a prepare() method.

If instead you remove the constructor and leave that to the base class, that will create the connection and assign it to $this->conn. One thing you will have to change is $conn needs to be defined as protected to allow the derived class to access it.

protected $conn;

Also ensure that when you execute(), this just returns if the execute has succeeded, the result comes from the fetch()

class insertData extends DBConnection
{
    public function getData()
    {
        $sql = "SELECT * FROM user";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $res = $stmt->fetch();
        print_r($res);
    }
}

Update:

To check what's happening, can you try...

ini_set('display_errors', 'On');
error_reporting(E_ALL);
$id = new insertData();
echo $id->getData();

Upvotes: 2

Related Questions