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();
11
  • It's very different on the link that tagged above @hanky panky Commented Jan 18, 2017 at 10:33
  • Original question said I'm getting error Notice: Undefined variable: connection in C:\xampp\htdocs\oop\views\index.php on line 13 so imho the duplicate flag is spot on it can help you with the solution quickly Commented Jan 18, 2017 at 10:35
  • What error do you get? Commented Jan 18, 2017 at 10:39
  • yes thanks for that, but it's very different since as looking for the link it's not a class OOP method. @HankyPanky Commented Jan 18, 2017 at 10:40
  • Notice: Undefined variable: connection in C:\xampp\htdocs\oop\views\index.php on line 13 Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\oop\views\index.php on line 13 . I don't know but the connection is declared . @MUNISHKUMAR Commented Jan 18, 2017 at 10:41

2 Answers 2

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'];
      }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.