0

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

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

5 Comments

yeah i was confused about it a bit ... but still same output
connected successfully from db class
Can you look at the last bit of the answer to enable errors and see if something happens.
Notice: Undefined property: insertData::$conn in /var/www/html/http/index.php on line 12 Fatal error: Uncaught Error: Call to a member function prepare() on null in /var/www/html/http/index.php:12 /var/www/html/http/index.php(22): insertData->getData() #1 {main} thrown in /var/www/html/http/index.php on line 12 thanks for the idea at least i see the error now
Make sure that in connection.php, you change protected $conn;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.