0

I'm getting another error with this code. It's:

Connected successfully Fatal error: Uncaught Error: Call to a member function query() on null in index5.php:29 Stack trace: #0 index5.php(44): User->getAllUsers() #1 index5.php(55): ViewUser->showAllUsers() #2 {main} thrown in index5.php on line 29

I'm trying to echo out data from my database table called "indeximg" but this code gives me the error above. I'm not sure how to fix this. This is my code:

<?php 
class Database {

 private $host = 'localhost';
 private $db_name = 'photos';
 private $username = 'root';
 private $password = '';
 private $conn;

 protected function connect() { 

  try {
   $this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
   $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   echo "Connected successfully"; 
  } catch(PDOException $e) {
   echo 'Connection Error: ' . $e->getMessage();
  }

  $this->conn = null;
 }

}

class User extends Database {
 
 protected function getAllUsers() {
  $sql = "SELECT * FROM indeximg";
  $result = $this->connect()->query($sql);
  $numRows = $result->num_rows;
  if ($numRows > 0) {
   while ($row = $result->fetch_assoc()) {
    $data[] = $row;
   }
   return $data;
  }
 }

}

class ViewUser extends User {
 
 public function showAllUsers() {
  $datas = $this->getAllUsers();
  foreach ($datas as $data) {
   echo $data['id']."<br>";
   echo $data['username']."<br>";
  }
 }

}


$users = new ViewUser();
$users->showAllUsers();

?>
tereško
  • 58,060
  • 25
  • 98
  • 150
codo7081
  • 25
  • 7
  • 1
    your connect() method doesn't return the connection... therefore you cannot chain the method as you did – Honk der Hase Sep 25 '18 at 20:10
  • I don't think a User should extend a Database, if you wanna learn more look at composition for the future ^^ – zebnat Sep 25 '18 at 20:11
  • As @zebnat mentioned, `User` is not a specialized type of a `Database`. As an occasional user of things, I find it quite dehumanizing. You might also find this useful: https://stackoverflow.com/a/11369679/727208 – tereško Sep 25 '18 at 20:14
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Sep 25 '18 at 20:38
  • @Lars Stegelitz Yes that's exactly what the problem was. I've fixed it now, thanks – codo7081 Sep 25 '18 at 20:46
  • @tereško Yes, I'll need to rework this entire code. I didn't quite understand the concept of Classes. I'll need to look into more tutorials on this. Thank you for your help! – codo7081 Sep 25 '18 at 20:46

2 Answers2

1

query() method called from null because you are returning nothing from the connect() function. Add a line as shown in the comment.

protected function connect() {  

try {
    $this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    return $this->conn;//Add this line
} catch(PDOException $e) {
    echo 'Connection Error: ' . $e->getMessage();
}

$this->conn = null;
}
Prakash S
  • 632
  • 6
  • 12
  • 1
    This fixed it! Thank you so much I was trying to resolve this all day yesterday. Thank you, thank you!! – codo7081 Sep 25 '18 at 20:24
0

There are a couple of issues in your connect() function:

First, you are setting $this->conn as null even when it connects successfully.

Second, you are chaining a function to the result of the connect() function that doesn't return anything:

protected function connect() 
{   
    try {
        $this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
    } catch(PDOException $e) {
        die('Connection Error: ' . $e->getMessage()); // Or do something else to handle the error
    }

    return $this->conn; 
}
  • Yes, this was the fix, thank you! I had a feeling it was that one null connect line. Thank you greatly for the advice!! – codo7081 Sep 25 '18 at 20:34