1

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

3
  • 2
    Obviously $this->connect() returns nothing Commented Oct 19, 2021 at 19:52
  • 1
    But you shouldn't really be connecting again every time you run a query... Commented Oct 19, 2021 at 20:37
  • dear u_mulder and ADyson Please read my answer and please let me know if you have a comment Commented Oct 19, 2021 at 20:50

2 Answers 2

1

As u_mulder said in the comments,

your connect method will not return anything.

I updated it as below and made some change on your getData() method:

 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); return new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass); } catch (PDOException $e) { $this->error = $e->getMessage(); echo $this->error; } } } class View extends Data { public function getData($table){ try { $sql="SELECT * FROM $table"; $q = $this->connect()->query($sql) or die("failed!"); while($r = $q->fetch(PDO::FETCH_ASSOC)){ $data[]=$r; } return $data; } catch(PDOException $e) { echo 'Query failed'.$e->getMessage(); } } } $view = new View; $result = $view->getData('equipment'); print_r($result); 

Although I prefer remove connect method and add a constructor in your Data class as below:

 public $this->conn; public function __construct(){ $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass); } 

and then change my getData as below:

$q = $this->conn->query($sql) or die("failed!"); 

‌Because as ADyson said in the comments :

you shouldn't really be connecting again every time you run a query..

Sign up to request clarification or add additional context in comments.

3 Comments

Read my comment please.
Great points here and I thank you for your fixes. I wish to better learn to work with php in this way so I want to understand the problem in my original code. I will also look into the construct approach. Thanks,
The problem was that you have not returned anything in the Connect method. Anyway, I'm glad my answer helped you
0

Thanks again Ali, ADyson & u_mulder,

If I understand what all comments are saying, this code should factor in your advice. Can you please tell me if this is a better approach if you have the time.

Data Class:

<?php class Data { private $dbHost = DB_HOST; private $dbUser = DB_USER; private $dbPass = DB_PASS; private $dbName = DB_NAME; private $error; public $this->conn; public function __construct(){ $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass); } try { return new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass); } catch (PDOException $e) { $this->error = $e->getMessage(); echo $this->error; } } } 

View Class:

<?php class View extends Data { public function getData($table){ try { $sql="SELECT * FROM $table"; $q = $this->conn->query($sql) or die("failed!"); while($r = $q->fetch(PDO::FETCH_ASSOC)){ $data[]=$r; } return $data; } catch(PDOException $e) { echo 'Query failed'.$e->getMessage(); } } } 

Output:

<?php $view = new View; $result = $view->getData('equipment'); print_r($result); ?> 

This code is still giving errors:

unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';'

3 Comments

for your new code you don't have wrote your code in a method! In fact after adding constructor you don't need that
So you mean I should remove the entire getData function and simply put: $sql="SELECT * FROM $table"; $q = $this->connect()->query($sql) or die("failed!"); while($r = $q->fetch(PDO::FETCH_ASSOC)){ $data[]=$r; } return $data; } Sorry, I am so new and I have not used constructors before so I am a bit confused
yes. If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.