Exemplo n.º 1
0
    /**
     * Initializes a database connection.
     * It returns the connection, if successful.
     *
     * @param string $db_server
     * @param string $db_name
     * @param string $db_user
     * @param string $db_passwd
     * @param string $db_prefix
     * @param mixed[] $db_options
     *
     * @return resource
     */
    public static function initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array())
    {
        global $mysql_set_mode;
        // Initialize the instance... if not done already!
        if (self::$_db === null) {
            self::$_db = new self();
        }
        // Non-standard port
        if (!empty($db_options['port'])) {
            $db_port = (int) $db_options['port'];
        } else {
            $db_port = 0;
        }
        // Select the database. Maybe.
        if (empty($db_options['dont_select_db'])) {
            $connection = @mysqli_connect((!empty($db_options['persist']) ? 'p:' : '') . $db_server, $db_user, $db_passwd, $db_name, $db_port);
        } else {
            $connection = @mysqli_connect((!empty($db_options['persist']) ? 'p:' : '') . $db_server, $db_user, $db_passwd, '', $db_port);
        }
        // Something's wrong, show an error if its fatal (which we assume it is)
        if (!$connection) {
            if (!empty($db_options['non_fatal'])) {
                return null;
            } else {
                display_db_error();
            }
        }
        // This makes it possible to automatically change the sql_mode and autocommit if needed.
        if (isset($mysql_set_mode) && $mysql_set_mode === true) {
            self::$_db->query('', 'SET sql_mode = \'\', AUTOCOMMIT = 1', array(), false);
        }
        self::$_db->_connection = $connection;
        // Few databases still have not set UTF-8 as their default input charset
        self::$_db->query('', '
			SET NAMES UTF8', array());
        return $connection;
    }