I can not use a query method when I use a connection class

0

My connection class:

class Connection{

    private $host="localhost";
    private $user="root";
    private $password="";
    private $dbname="test";         

    private function conectionDB(){
        $conn = new PDO("mysql:host=$this->host;dbname=$this->dbname","$this->user", "$this->password");
        return $conn;           
    }

    public function Connection(){
        $this->conectionDB();           
    }
}

How I am performing my call in index.php:

<?php   
require_once('class/Connection.class.php');
$pdo_conn = new Connection();
$query = "select * from qualquercoisa"
$pdo_statement = $pdo_conn->prepare($query);

?>

The error that presents me:

  

Fatal error: Uncaught Error: Call to undefined method Connection :: prepare ()

    
asked by anonymous 27.05.2017 / 19:27

1 answer

0

When you use $pdo_conn = new Connection(); you only instantiate an object of the Connection class, you are not creating the connection to your database. To get your connection to the database, do the following:

<?php   
  $pdo_conn = $pdo_conn->conectionDB();
?>
    
31.05.2017 / 16:29