tags:

views:

79

answers:

3

When building dynamic websites I use a php include to make my connections to the database. The included file is very basic:

mysql_connect($hostname = 'host', $username = 'user', $password = 'password'); mysql_select_db('database');

This works fine.

In some places I use an AJAX system to create drag-and-drop reordering of database records which updates the database at the same time, it is adapted from something I found on the internet. This uses its own connection code:

class SortableExample { protected $conn;
protected $user = 'user';
protected $pass = 'password';
protected $dbname = 'database';
protected $host = 'host';
public function __construct() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn);
}

This also woks fine.

What it means, however, is that I have to add the username, password, host and database to two separate files. Sometimes the second one is forgotten and causes the website to fail.

My question is, how can I either combine both connection files into one, OR how can I get the second block of code to accept external variables so that I only have to enter the actual values in one place?

A: 

You can simply put the database connection code in another file and included that everywhere you need it.

Franz
That's what I normally do, but the file that has the creation of a class in it was where I couldn't get my already created connection to come into play. Thanks to Kolky's answer, though, it now works.
AndrewDFrazier
You can include code inside a class function, too ;)
Franz
+1  A: 

Your last question is easy.

db.config.php

$host = '';
$user = '';
$pass = '';
$db = '';

db.plain.php

include 'db.config.php';

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db,$conn);

db.class.php

include 'db.config.php';

class SortableExample
{
  protected $conn;
  public function __construct()
  {
    global $host, $user, $pass, $db;
    $this->conn = mysql_connect($host, $user, $pass);
    mysql_select_db($db,$this->conn);
  }
}
Kolky
That does the trick. I was nearly there in my own attempts, but I'd forgotten to use global in front of the variables.
AndrewDFrazier
A: 

Create a single entry point for your database connection.

Use a Singleton with lazy instantiation for that:

class ConnectionProvider { 
    protected $conn;
    protected $user = 'user';
    protected $pass = 'password';
    protected $dbname = 'database';
    protected $host = 'host';
    private static $__instance;

    private function __construct() {
        $this->conn = mysql_connect($this->host, $this->user, $this->pass);
        mysql_select_db($this->dbname,$this->conn);
    }

    public static function getInstance() {
        if ( self::$__instance == null) {
            self::$__instance = new ConnectionProvider();
        }
        return self::$__instance;
    }

    public function getConnection() {
        return $this->conn;
    }
}

And then, from your code

ConnectionProvider::getInstance()->getConnection();

to use the connection wherever you need it.

SortableExample would thus become:

class SortableExample { 
    protected $conn;
    public function __construct() {
        $this->conn = ConnectionProvider::getInstance()->getConnection();
    }
    ...
}
Silvio Donnini
I like the look of this but it I think I'd have to alter to many pages in my website that call my normal database connection. I'm going to look into it properly when I have more time, though, because I think I should be using more code like this and less of the basic stuff that I create.
AndrewDFrazier