简体   繁体   English

如何合并多个php / mysql数据库连接?

[英]How to combine multiple php/mysql database connections?

When building dynamic websites I use a php include to make my connections to the database. 建立动态网站时,我使用php include来建立与数据库的连接。 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. 在某些地方,我使用AJAX系统创建数据库记录的拖放重新排序,该更新同时更新数据库,它是根据我在互联网上找到的内容改编而成的。 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? 我的问题是,如何将两个连接文件合并为一个,或者如何获取第二个代码块来接受外部变量,以便只需要在一个地方输入实际值?

Your last question is easy. 您的最后一个问题很简单。

db.config.php db.config.php

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

db.plain.php db.plain.php

include 'db.config.php';

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

db.class.php 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);
  }
}

您可以简单地将数据库连接代码放在另一个文件中,并将其包含在您需要的任何位置。

Create a single entry point for your database connection. 为您的数据库连接创建一个入口点。

Use a Singleton with lazy instantiation for that: 为此,请使用具有延迟实例化的Singleton:

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: 这样,SortableExample将变为:

class SortableExample { 
    protected $conn;
    public function __construct() {
        $this->conn = ConnectionProvider::getInstance()->getConnection();
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM