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 __contruct(){
    return $this->conectionDB();           
}
}

My index.php:

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

 ?>

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

rock.ownar
  • 113
  • 3
  • This is not duplicated – rock.ownar May 27 '17 at 20:31
  • two things: 1) you're using a construct method that is deprecated in php7; use __construct instead of the class name. 2) You forgot to put a `return $this` in the construct method. I think you've got a good start; I'd go ahead and make `Connection` into an abstraction, which would allow you to create find, update, insert, and delete methods which would simplify your code considerably. FWIW, this post does not look like a newbie to me; it very well could be a ruby programmer trying to use php. This mistake has bitten me a couple of times... – Tim Morton May 27 '17 at 20:54
  • I modified, but I still have a problem. – rock.ownar May 27 '17 at 20:59
  • you made 2 typos and php's error reporting would have helped you here. – Funk Forty Niner May 27 '17 at 21:00
  • to which and to mark the question as properly duplicated, another was added. – Funk Forty Niner May 27 '17 at 21:06

0 Answers0