Actions

API15

JDatabaseMySQLi/ construct

From Joomla! Documentation

< API15:JDatabaseMySQLi
Revision as of 17:15, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Database object constructor

[Edit Descripton]

Description:JDatabaseMySQLi/ construct

Syntax

__construct($options)
Parameter Name Default Value Description
$options List of options used to configure the connection

Defined in

libraries/joomla/database/database/mysqli.php

Importing

jimport( 'joomla.database.database.mysqli' );

Source Body

function __construct( $options )
{
        $host           = array_key_exists('host', $options)    ? $options['host']              : 'localhost';
        $user           = array_key_exists('user', $options)    ? $options['user']              : '';
        $password       = array_key_exists('password',$options) ? $options['password']  : '';
        $database       = array_key_exists('database',$options) ? $options['database']  : '';
        $prefix         = array_key_exists('prefix', $options)  ? $options['prefix']    : 'jos_';
        $select         = array_key_exists('select', $options)  ? $options['select']    : true;
 
        // Unlike mysql_connect(), mysqli_connect() takes the port and socket
        // as separate arguments. Therefore, we have to extract them from the
        // host string.
        $port   = NULL;
        $socket = NULL;
        $targetSlot = substr( strstr( $host, ":" ), 1 );
        if (!empty( $targetSlot )) {
                // Get the port number or socket name
                if (is_numeric( $targetSlot ))
                        $port   = $targetSlot;
                else
                        $socket = $targetSlot;
 
                // Extract the host name only
                $host = substr( $host, 0, strlen( $host ) - (strlen( $targetSlot ) + 1) );
                // This will take care of the following notation: ":3306"
                if($host == '')
                        $host = 'localhost';
        }
 
        // perform a number of fatality checks, then return gracefully
        if (!function_exists( 'mysqli_connect' )) {
                $this->_errorNum = 1;
                $this->_errorMsg = 'The MySQL adapter "mysqli" is not available.';
                return;
        }
 
        // connect to the server
        if (!($this->_resource = @mysqli_connect($host, $user, $password, NULL, $port, $socket))) {
                $this->_errorNum = 2;
                $this->_errorMsg = 'Could not connect to MySQL';
                return;
        }
 
        // finalize initialization
        parent::__construct($options);
 
        // select the database
        if ( $select ) {
                $this->select($database);
        }
}

[Edit See Also] SeeAlso:JDatabaseMySQLi/ construct

Examples

<CodeExamplesForm />