1

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();
Community
  • 1
  • 1

2 Answers2

3

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'];
      }
  }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Andrew
  • 12,617
  • 1
  • 34
  • 48
  • *Cheers* Andrew. As I told the OP earlier; I didn't see your answer posted here where I asked her if the question was still open so I took the time to test it and figure out where she went wrong, and was in the process of typing it all up. Your answer explains it very well. – Funk Forty Niner Jan 18 '17 at 17:46
0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks Free -ii- I just reprogram the code and run it. If u see my other Question AGAIN.. haha its different from here. But the other question is more on Controller which I don't know how it works. I just want to learn MVC slowly. – Genina Anne Gabuten Jan 18 '17 at 17:39
  • TBH, I had already written this up before seeing the other answer. I'm not kicking for rep here *lol* just saying. @GeninaAnneGabuten I'm just glad that you found your full solution. You're welcome *cheers* – Funk Forty Niner Jan 18 '17 at 17:40