全局变量 - 数据库连接? [英] Global Variable - database connection?
问题描述
我试图连接到一个数据库(MySQLi)只是一次,但是有这样的问题。
如何使连接全局的脚本?有多个文件(index.php,/classes/config.class.php,/classes/admin.class.php等)。
我试过以下内容:
In:config.class.php
$ 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会导致以下错误:
$ b $ b 警告:mysqli :: real_escape_string()[mysqli.real-escape-string]:无法在/home/calico/_/_.com/_中获取mysqli /index.php on line 20
警告:mysqli :: query()[mysqli.query]:无法在/home/calico/_/_.com/_/index中获取mysqli。 php on line 28
解决方案我个人使用单例类。像这样:
<?php
类数据库{
private static $ db;
private $ connection;
私有函数__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() code>无论我需要什么。
I am trying to connect to a database (MySQLi) just once, but am having problems doing so.
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).
I've tried the following:
In: 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
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();
I really am puzzled at how I'm to do this. Can anyone help?
--- Edit ---
This is my new config.class.php file:
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:
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
解决方案 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.
这篇关于全局变量 - 数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!