phpmysqlglobalhandlemysql-connect

PHP - How to get mysqli_connect() to return a RESOURCE so it can be used with define()


I have noticed something while testing different MySQL connection methods in PHP.

I usually use odbc_connect() when making a connection to a database in PHP. I save the resouce variable that odbc_connect() returns into a global constant (using the define() function) that I can conveniently access throughout my application. Like so:

Using odbc_connect() and saving the return value into a constant works fine:

<?php
define("_conn", odbc_connect("Driver={MySQL ODBC 5.3 Unicode Driver};Database=MyDB;", "user", "pass"));
?>

Saving the return value of mysql_connect() (the deprecated one) into a constant also works fine:

<?php
define("_conn", mysql_connect("localhost", "user", "pass", "MyDB"));
?>

However, trying to save the return value of mysqli_connect() into a constant does NOT work:

<?php
define("_conn", mysqli_connect("localhost", "user", "pass", "MyDB"));
?>
Warning: Constants may only evaluate to scalar values in C:\...\script.php on line 164

This is unfortunate, as it would be nice to use mysqli_connect() to establish a connection and save the handle into a constant, where odbc_connect() isn't available. I did research and found that the only two database connection functions that I can use with MySQL that return a RESOUCE (and can be used with the define() function) are odbc_connect() and mysql_connect() (the deprecated one). See this link: http://php.net/manual/en/resource.php

Is there a way to get mysqli_connect() to return a RESOUCE, so that I can use its return value in a constant (using the define() function)?

PDO does not return a RESOUCE either.


Solution

  • i would recommend to use the singleton pattern for this case Here a example:

    <?php
     class PDOConnection {
    
    /**
     * singleton instance
     * 
     * @var PDOConnection 
     */
    protected static $_instance = null;
    
    /**
     * Returns singleton instance of PDOConnection
     * 
     * @return PDOConnection 
     */
    public static function instance() {
    
        if ( !isset( self::$_instance ) ) {
    
            self::$_instance = new PDOConnection();
    
        }
    
        return self::$_instance;
    }
    
    /**
     * Hide constructor, protected so only subclasses and self can use
     */
    protected function __construct() {}
    
    function __destruct(){}
    
    /**
     * Return a PDO connection using the dsn and credentials provided
     * 
     * @param string $dsn The DSN to the database
     * @param string $username Database username
     * @param string $password Database password
     * @return PDO connection to the database
     * @throws PDOException
     * @throws Exception
     */
    public static function getConnection() {
    
    
        $dsn = 'mysql:dbname=_____;host=_____';
        $username = '_____';
        $password = '_____';
    
        $conn = null;
        try {
    
            $conn = new \PDO($dsn, $username, $password);
    
            //Set common attributes
            $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    
            return $conn;
    
        } catch (PDOException $e) {
    
            //TODO: flag to disable errors?
    
    
        }
        catch(Exception $e) {
    
            //TODO: flag to disable errors?
    
    
        }
    }
    
    /** PHP seems to need these stubbed to ensure true singleton **/
    public function __clone()
    {
        return false;
    }
    public function __wakeup()
    {
        return false;
    }
    }
    
    ?>
    

    Then you can use it from anywhere:

    $dbh =  PDOConnection::getConnection();