Genina Anne Gabuten
Genina Anne Gabuten

Reputation: 278

Getting query error in OOP php

I'm just new using OOP php and I'm having a hard time to figure out how to query in database using class method. Here's my code and I've got an error which I dont know how to solve.

I declared the connection variable but I don't know why it's undefined

Notice: Undefined variable: connection

Fatal error: Call to a member function query() on a non-object in

db.php

class DbConnector {

   private $serverName;
   private $userName;
   private $password;
   private $dbName;
   private $connection;

   public function __construct(){
      $this->serverName = "localhost";
      $this->userName = "root";
      $this->password = "attl";
      $this->dbName = "oop";

      $this->connection = new mysqli($this->serverName, $this->userName, $this->password, $this->dbName);

      if ($this->connection->connect_error) {
          $this->connection = die("Connection failed: " . $this->connection->connect_error);
      } 
  }

  public function getConnection(){
      return $this->connection;
  }
}

index.php

include('../queries/db.php');

class Users{

  private $connection;

  public function __construct(){
      $con = new DbConnector();
      $connection = $con->getConnection();
  }
  public function getUsers(){
      $sql = $connection->query("SELECT * FROM login");

      while($getUsers = $sql->fetch_array()){
          echo $getUsers['username'];
      }
  }

}

$user = new Users();
return $user->getUsers();

Upvotes: 1

Views: 638

Answers (2)

Andrew
Andrew

Reputation: 12809

Your problem is you're trying to access a locally scoped variable rather than a class property

public function getUsers(){
  $sql = $connection->query("SELECT * FROM login"); // HERE

  while($getUsers = $sql->fetch_array()){
      echo $getUsers['username'];
  }
}

this can't find a variable called $connection in that scope, you need to access the object property using $this.

class Users {

  private $connection;

  public function __construct()
  {
      $con = new DbConnector();
      // Assign this to object propety declared above 
      $this->connection = $con->getConnection();
  }

  public function getUsers()
  {
      // now access the object property set in constructor.
      $sql = $this->connection->query("SELECT * FROM login");

      while($getUsers = $sql->fetch_array()){
          echo $getUsers['username'];
      }
  }
}

Upvotes: 3

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Since you're declaring the following as private:
private $connection;

and including it in your construct:

  public function __construct(){
      $con = new DbConnector();
      $connection = $con->getConnection();

You need to use $this for its property:

$this->connection = $con->getConnection();

Then changing:

$sql = $connection->query

to

$sql = $this->connection->query

in order to use the connection property.

Now, I have to admit that I am not an OOP expert and there may be another way to have solved this, yet this is what I took from it, which worked for me.

Upvotes: 0

Related Questions