【问题标题】:Global Variable - database connection?全局变量 - 数据库连接?
【发布时间】:2011-10-17 11:17:54
【问题描述】:

我只尝试连接到数据库 (MySQLi) 一次,但这样做时遇到了问题。

如何为整个脚本建立一个全局连接?有多个文件(index.php、/classes/config.class.php、/classes/admin.class.php 等)。

我尝试了以下方法:

在:config.class.php

public static $config = array();
public static $sql;

function __construct() {
    // database
    db::$config['host'] = 'localhost';
    db::$config['user'] = '_';
    db::$config['pass'] = '_';
    db::$config['db'] = '_';

    // connect
    db::$sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
}

同样,在 config.class.php 中

public function contectToDatabase($sql){
    $sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
    $this->sql = $sql;
}

我使用具有以下代码的类: $config = new db();

我真的很困惑我该怎么做。有人可以帮忙吗?

--- 编辑 --- 这是我的新 config.class.php 文件:

public static $config = array();
public static $sql;

private static $db;
private $connection;

public function __construct() {
    // database
    db::$config['host'] = '_';
    db::$config['user'] = '_';
    db::$config['pass'] = '_';
    db::$config['db'] = '_';
    // connect
    $this->connection = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
}
function __destruct() {
    $this->connection->close();
}
public static function getConnection() {
    if($db == null){
        $db = new db();
    }
    return $db->connection;
}

这就是我加载它的方式:

require_once("classes/config.class.php");
$config = new db();
$sql = db::getConnection();

但是,运行 real_escape_string 会导致以下错误:

Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch mysqli in /home/calico/_/_.com/_/index.php on line 20

Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in /home/calico/_/_.com/_/index.php on line 28

【问题讨论】:

  • 你可以使用singleton pattern
  • 或者你可以学习依赖注入而不是使用singleton antipattern
  • 是的……单身人士总是会引起激烈的争论。我只是提供意见和想法
  • 有没有办法让连接作为每个类的全局变量?例如:我设置了一个名为 $sql 的全局变量,并将其设置为 $sql = new mysqli(db::$config['host'], db::$config['user'], db::$config[ '通过'], db::$config['db']);?我不想为每个班级重复数据库连接。

标签: php class mysqli global connect


【解决方案1】:

就个人而言,我使用单例类。像这样的:

<?php

class Database {

    private static $db;
    private $connection;

    private function __construct() {
        $this->connection = new MySQLi(/* credentials */);
    }

    function __destruct() {
        $this->connection->close();
    }

    public static function getConnection() {
        if (self::$db == null) {
            self::$db = new Database();
        }
        return self::$db->connection;
    }
}

?>

然后在我需要的地方使用$db = Database::getConnection();

【讨论】:

  • 你的 Singleton 可以被序列化和克隆,这意味着它不能确保只有一个实例。
  • 好的,我已经将它添加到配置类中,并尝试加载它。但是,存在错误(请参阅主帖,在 ---edit--- 行下)。有什么想法有什么问题吗?
  • @Peter 不要将它添加到另一个类。这是一个单例类,应该是独立的,不应该有公共构造函数。只需使用$db = Database::getConnection(); 即可在任何需要的地方获取一个开放的 MySQLi 实例。
  • @Gordon 是的,这只是一个简单的例子;因此“像这样”
  • 我可以用这个实现 PDO 吗?
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 2013-09-29
相关资源
最近更新 更多