Jump to content

[SOLVED] Calling database object from another class


Cep

Recommended Posts

Hello!

 

I am using the MySQLi extension to create database connections. I have created a new connection using,

 

$db = new mysqli('server', 'user', 'pass', 'database');

 

I am now creating new classes that will need to utilise the database I am working with, can I make calls to $db from within these classes for new objects?

 

Any help would be great cheers :)

you can do it two ways

 

either in each function declare it as global (not recommeded)

 

global $db;

 

or

 

make it a member of the new class

 

$objNewclass->db = $db;

 

and then call it in the class

$this->db->dbfunction(....)

 

hope its helpful

 

 

I'd like to take your second approach, I want to avoid the global one.

 

Little hazy though,

 

so

 

$db = new mysqli('server', 'user', 'pass', 'database');

class MyClass () {

public function db() {

public function selecttable() {
	if ($result = $this->db->query('SELECT * FROM table')) {
		while ($row = $result->fetch_assoc()) {
			echo $row['field'];
		}
	}
} 
}

$newObject = new MyClass;

$newObject->selecttable;

}

 

Little confused. Do you mean set it as a propertie? I thought you wrote Method.

 

 

<?php

class MyClass () {

  private $db;

  function __construct() {
    $this->db = new mysqli('server', 'user', 'pass', 'database');
  }

  public function selecttable() {
    if ($result = $this->db->query('SELECT * FROM table')) {
      while ($row = $result->fetch_assoc()) {
        echo $row['field'];
      }
    }
  }
}

$newObject = new MyClass;

$newObject->selecttable;

?>

Just an update to this question, I have successfully managed to adopt rajivgonsalves idea which is what I wanted to do.

 

<?php

$db = new mysqli('server', 'user', 'pass', 'database');


class MyClass () {

  public $db;
  public $field = "";


  public function selecttable($id) {
    if ($result = $this->db->query("SELECT * FROM table WHERE id = {$id}")) {
      while ($row = $result->fetch_assoc()) {
         $this->field = $row['field'];
      }
    }
  }
}

$newObject = new MyClass;

$newObject->db = $db;

$newObject->selecttable(1);

$field = $newObject->field;

echo $field;

?>

 

Now my question is, which method is best practice?

 

The above or the one Thorpe mentions whereby you create a new connection for each class? Am I right in thinking that by using the above code I am creating one connection object and passing that to any class object that requires it? Or is Thorpe's method better?

Creating a new mysqli object will create a new connection. What you really ought to do is create a singleton, so that your only ever using the one mysqli object.

 

<?php

  class myMySqli {
    
    private static $instant;

    private function __construct() {}

    public static function getInstance() {
      if (!isset(self::$instant)) {
        self::$instant = new mysqli('server', 'user', 'pass', 'database');
      }
      return self::$instant;
    }
  }

  class MyClass () {

    public $field = "";

    public function selecttable($id) {
      if ($result = myMySqli::getInstance()->query("SELECT * FROM table WHERE id = {$id}")) {
        while ($row = $result->fetch_assoc()) {
          $this->field = $row['field'];
        }
      }
    }
  }

?>

 

I just did'nt really want to get into this earlier.       

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.