I cannot use a query method when using a connection class

Asked

Viewed 30 times

0

My kind of connection:

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’m making my call on index.php:

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

?>

The mistake you present to me:

Fatal error: Uncaught Error: Call to Undefined method Connection:()

1 answer

0

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

<?php   
  $pdo_conn = $pdo_conn->conectionDB();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.