0

I am fairly new to PHP and I have found that a lot of my functions require access to the database. The problem is I currently connect to the database in a file that I require at the top of every page like so:

$db = new mysqli("localhost", "my_user", "my_password", "world");

The problem I find is then when I want to access it within a function I have to pass the $db variable to it... which works fine, but is there an easier way?

0

3 Answers 3

0

The easiest (but not the best) way is to add 'global' keyword in each function. Example:

$db = new mysqli("localhost", "my_user", "my_password", "world");
function foo() {
  global $db;
  $db->query(....
}

Much better way is to create your own class My_DB and define it as singleton. Then you will be able to call db object as an instance of the My_DB.

class My_DB {
  protected $_db = null;

  protected static $instance = null;

  protected function __construct() {
    $this->_db = new mysqli("localhost", "my_user", "my_password", "world");
  }

  public static function getInstance() {
    if (empty(self::$_instance)) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }
}

And then anywhere in your code you can do the following:

function foo() {
  $db = My_DB::getInstance();
  $db->query(....
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it looks great... you say the second is 'much better' ... can I ask what the advantage is?
The second is better because global variable is accessible in any part of your code, so some third-party library (or your code) could redefine it. But you can't redefine class - this will cause fatal error.
0

You can call them as functions on the $db object, e.g. $db->query(...).

4 Comments

I understand this much... my problem is not getting $db to work.. it is that everytime I call a function I have to add $db as one of the variables that I pass to it... e.g FunctionName($var1,$var2,$db); is there not a better way to make $db accessable?
You want $db to be a global variable. If you put an include file that defines $db outside the scope of any function, it will be available in any file that includes it. If you want a more object-oriented method, have an include that creates a $db that's a singleton instance variable of a database handle.
you lost me at 'singleton instance variable of a database handle.' .. sounds good though... any website that I can read?
php.net/manual/en/class.mysqli.php The docs are actually pretty good for this class, and should give you everything you need. You want the object approach, not the procedural one.
0

Take a look at doctrine. There is a little bit of ramp up but it is very nice.

Otherwise put that code in an include file and include "db.php"; in your pages. Preferable you have 1 include file that gets you everything you need for your site, and you add the $db code there.

If you really want to be quick and dirty, make $db a global and then you can access it anywhere. This isn't great coding practice.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.