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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7792974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:22:30  来源:igfitidea点击:

Global Variable - database connection?

phpclassmysqliglobalconnect

提问by Peter

I am trying to connect to a database (MySQLi) just once, but am having problems doing so.

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

How do I make a connection global for the entire script? There are multiple files (index.php, /classes/config.class.php, /classes/admin.class.php, etc).

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

I've tried the following:

我尝试了以下方法:

In: config.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']);
}

Again, in config.class.php

再次,在 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;
}

I use the class with the following code: $config = new db();

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

I really am puzzled at how I'm to do this. Can anyone help?

我真的很困惑我该如何做到这一点。任何人都可以帮忙吗?

--- Edit --- This is my new config.class.php file:

--- 编辑 --- 这是我的新 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;
}

And this is how I'm loading it:

这就是我加载它的方式:

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

However, running a real_escape_string results in the following errors:

但是,运行 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

回答by daiscog

Personally, I use a singleton class. Something like this:

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

<?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;
    }
}

?>

Then just use $db = Database::getConnection();wherever I need it.

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