Php 全局变量-数据库连接?

Php 全局变量-数据库连接?,php,class,mysqli,global,connect,Php,Class,Mysqli,Global,Connect,我尝试只连接一次数据库(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::$con

我尝试只连接一次数据库(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']);
}
public function contectToDatabase($sql){
    $sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
    $this->sql = $sql;
}
同样,在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']);
}
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();
但是,运行真正的_escape_字符串会导致以下错误:

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

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

<?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()我需要的任何地方。

你可以使用,或者你可以学习依赖注入,而不是使用是的……单例总是会引起激烈的争论。我只是提供输入和想法,有没有办法让连接成为每个类的全局变量?例如:我设置了一个名为$sql的全局变量,并将其设置为$sql=newmysqli(db::$config['host'],db::$config['user'],db::$config['pass'],db::$config['db']);?我不想为每个类重复数据库连接。您的单例可以序列化和克隆,这意味着它不能确保只有一个实例。好的,我已将其添加到config类中,并尝试加载它。但是,也有一些错误(请参阅主贴子中的--edit--行)。有什么想法吗?有什么问题吗?@Peter不要把它添加到另一个类中。这是一个单例类,应该是独立的,不应该有公共构造函数。只需使用
$db=Database::getConnection()
在任何需要的地方获取一个开放的MySQLi实例。@Gordon是的,这只是一个简单的例子;因此,“类似这样的东西”我可以用这个实现PDO吗?