3

I am new to using PHP in an OOP way but have found 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);

I then want to use the variable $mysqli in a db connection in another file - this is a simple insert to a database using $mysqli variable to connect. I included the above in the connection file but it seems the $mysqli variable is not being returned when I call the method within the database class. I get the PHP error saying...

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

I have seen that using

global $mysqli; 

works however I want to do it the proper way as I have heard that is not good practice.

I understand that I am potentially doing things a bit wrong here as I'm new to using OOP but I assumed that by returning that variable in the connect function I can then access it from creating the class outside.

Help is appreciated, thanks.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Andre
  • 233
  • 8
  • 24

3 Answers3

3

You need to change:

$mysqli->connect_errno

To:

$this->mysqli->connect_errno
hohner
  • 11,498
  • 8
  • 49
  • 84
3

When using it outside , you access a class variable via the instance...

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

from inside you use the keyword $this...

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

In case you want to protect your variable from outside access use:

  private $mysqli; 
  // instead of 
  public $mysqli;
axel.michel
  • 5,764
  • 1
  • 15
  • 25
  • hey dude. I slightly altered your answer to '$mysqli = $newConnection->mysqli' and it works. If you want to change your answer I'll up vote it :) thanks a lot. – Andre Jan 26 '13 at 15:13
  • @Andre or you use '$newConnection->mysqli' directly instead of transfering its value to another variable named $mysqli. – axel.michel Jan 26 '13 at 15:43
1
Fatal error: Call to a member function prepare() on a non-object in...

this always means, the the thing you called the method on, is not an object. In your case: mysqli is not initialized.

a general tip: connect looks like something, that should be within the 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 aquire an instance of this class, it's automatically initialized. Also this way you save a line, each time you want to initialize it.

scones
  • 3,317
  • 23
  • 34