Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variable - database connection?

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
like image 753
Peter Avatar asked Oct 17 '11 11:10

Peter


People also ask

How do I create a global database connection?

Approach 1: Use Global Variable Declare a global variable DB of type *sql. DB to hold the database connection. Write a function that will open the connection and assign it to the global variable. Now, where ever you need to access the database, you can simply import the global variable and start using it.

What is global variable in database?

A global variable is a named memory variable that you access through SQL statements. Global variables let you share relational data between SQL statements without the need for application logic to support this data transfer.

Can global variables be accessed anywhere?

You can access the global variables from anywhere in the program. However, you can only access the local variables from the function. Additionally, if you need to change a global variable from a function, you need to declare that the variable is global. You can do this using the "global" keyword.

How do I create a global database connection in PHP?

<? php //Connect To Database function connect_to_database() { require_once 'config. php'; global $db_host; global $db_database; global $db_user; global $db_pass; $db_server = mysqli_connect($db_host, $db_user, $db_pass); if (!$ db_server) die("Unable to connect to MySQL: " .


1 Answers

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.

like image 136
daiscog Avatar answered Oct 03 '22 17:10

daiscog