I am using PHP to establish a connection to a MYSQL database using PDO. I keep getting this error:
Uncaught Error: Call to a member function query() on null
I am pretty new to this approach but why am I getting a null from the query?
This is the code calling the classes:
<?php $data = new Data; echo $data->connect(); $view = new View; echo $view->getData(); ?> This is the query class with the problem I suspect:
<?php class View extends Data { public function getData() { $sql = 'SELECT * FROM equipment'; $stm = $this->connect()->query($sql); while ($row = $stm->fetch()) { echo $row['manuName'] . '<br>'; } } } ?> This is the connection class:
<?php class Data { private $dbHost = DB_HOST; private $dbUser = DB_USER; private $dbPass = DB_PASS; private $dbName = DB_NAME; private $dbHandler; private $error; public function connect() { $con = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName; $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); try { $this->dbHandler = new PDO($con, $this->dbUser, $this->dbPass, $options); } catch (PDOException $e) { $this->error = $e->getMessage(); echo $this->error; } } } I am not seeing my error. If anyone can see why I cannot pull the data.
Thank you
$this->connect()returns nothing