PHP mysqli connection class - cannot access the connection variable from outside. Scope of application

I am new to using PHP in OOP but ran into an issue with my database connection class.

I have a file with this mysqli connection class here

$db_name = 'dbname';
$db_user = 'dbuser';        
$db_password = 'dbpassword';
$db_host = 'localhost';

class database {

    public $mysqli;

    public function connect($db_host, $db_user, $db_password, $db_name){

        $this->mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

        if ($mysqli->connect_errno) {
            return "Sorry Andre, but you seem to have messed up the DB connection :(";
        }
    }
}

$newConnection = new database;
$newConnection->connect($db_host, $db_user, $db_password, $db_name);

      

Then I want to use the $ mysqli variable in the db connection in another file - this is a simple insert into the database using the $ mysqli variable to connect. I have included the above in the connection file, but it seems that the $ mysqli variable is not returned when I call the method on the database class. I am getting PHP error ...

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

      

I saw that using

global $mysqli; 

      

works, however I want to do it properly, as I heard it is not good practice.

I realize that I am potentially doing things a little wrong here as I am new to using OOP, but I assumed that by returning this variable in the connect function, I could access it from creating the class outside.

Help rate, thanks.

+3


source to share


3 answers


When used externally, you access the class variable through an instance ...

$newConnection = new database;
$newConnection->connect($db_host, $db_user, $db_password, $db_name);
$newConnection->mysqli /* here you have access from outside */

      

internally you are using the $ this keyword ...



// like this from inside
if ($this->mysqli->connect_errno) {
    return "Sorry Andre, but you seem to have messed up the DB connection :(";
}

      

If you want to protect your variable from external access use:

  private $mysqli; 
  // instead of 
  public $mysqli;

      

+3


source


You need to change:

$mysqli->connect_errno

      



To:

$this->mysqli->connect_errno

      

+3


source


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

      

This always means that the thing you called the on method is not an object. In your case: mysqli is not initializing.

general advice: connect looks like something that should be inside a constructor.

class d {
  public function __construct($db_host, $db_user, $db_password, $db_name){
    $this->mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
    if ($this->mysqli->connect_errno) {
      return "Sorry Andre, but you seem to have messed up the DB connection :(";
    }
  }

  public $mysqli;
}

$foo = new d('host', 'user', 'pass', 'dbname');
$foo->mysqli->prepare("something");

      

So when you acquire an instance of this class, it is automatically initialized. Also this way you save the string every time you want to initialize it.

+1


source







All Articles