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 :)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

<?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;

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.       

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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