Part of the EllisLab Network
   
 
DB can’t recognize an Array parameter
Posted: 05 May 2008 06:49 AM   [ Ignore ]  
Summer Student
Total Posts:  7
Joined  2008-01-28

In CI 1.6.1 database/DB.php

function &DB;($params = '', $active_record_override = FALSE)
{
    
// Load the DB config file if a DSN string wasn't passed
    
if (is_string($params) AND strpos($params, '://') === FALSE)
    
{
        
include(APPPATH.'config/database'.EXT);
        
        if ( ! isset(
$db) OR count($db) == 0)
        
{
            show_error
('No database connection settings were found in the database config file.');
        
}
        
        
if ($params != '')
        
{
            $active_group
= $params;
        
}
        
        
if ( ! isset($active_group) OR ! isset($db[$active_group]))
        
{
            show_error
('You have specified an invalid database connection group.');
        
}
        
        $params
= $db[$active_group];            
    
}
    
else
    
{
        
        
/* parse the URL from the DSN string
        *  Database settings can be passed as discreet
         *  parameters or as a data source name in the first
         *  parameter. DSNs must have this prototype:
         *  $dsn = 'driver://username:password@hostname/database';
        */
    
        
if (($dns = @parse_url($params)) === FALSE)
        
{
            show_error
('Invalid DB Connection String');
        
}
        
        $params
= array(
                            
'dbdriver'    => $dns['scheme'],
                            
'hostname'    => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
                            
'username'    => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
                            
'password'    => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
                            
'database'    => (isset($dns['path'])) ? rawurldecode(substr($dns['host'], 1)) : ''
                        
);
    
}
    
    
// No DB specified yet?  Beat them senseless...
    
if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
    
{
        show_error
('You have not selected a database type to connect to.');
    
}

    
// Load the DB classes.  Note: Since the active record class is optional
    // we need to dynamically create a class that extends proper parent class
    // based on whether we're using the active record class or not.
    // Kudos to Paul for discovering this clever use of eval()
    
    
if ($active_record_override == TRUE)
    {
        $active_record
= TRUE;
    
}
    
    
require_once(BASEPATH.'database/DB_driver'.EXT);

    if (! isset(
$active_record) OR $active_record == TRUE)
    
{
        
require_once(BASEPATH.'database/DB_active_rec'.EXT);
        
        if ( !
class_exists('CI_DB'))
        
{
            eval(
'class CI_DB extends CI_DB_active_record { }');
        
}
    }
    
else
    
{
        
if ( ! class_exists('CI_DB'))
        
{
            eval(
'class CI_DB extends CI_DB_driver { }');
        
}
    }
            
    
require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);

    
// Instantiate the DB adapter
    
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    
$DB =& new $driver($params);
    
    if (
$DB->autoinit == TRUE)
    
{
        $DB
->initialize();
    
}
    
    
return $DB;
}

and in Database Configuration Docs, CI tell us:

To connect manually to a desired database you can pass an array of values:
$config[’hostname’] = “localhost”;
$config[’username’] = “myusername”;
$config[’password’] = “mypassword”;
$config[’database’] = “mydatabase”;
$config[’dbdriver’] = “mysql”;
$config[’dbprefix’] = “”;
$config[’pconnect’] = FALSE;
$config[’db_debug’] = TRUE;

$this->load->database($config);

But what happened when I just do the docs told me what to do? CI can’t connect to the database, since it can not get the database configuration correctly. Corresponding to the code, the DB function just support two kind of parameter ‘$params’, that is
1.config group name
2.DSN name

if you pass a config array, it will be used as a DSN name, and you can’ t connect to the db.

Profile
 
 
Posted: 05 May 2008 06:49 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  7
Joined  2008-01-28

what should we do is just leave it when the $param is a array type. and the code should changed to

function &DB;($params = '', $active_record_override = FALSE)
{
    
if (is_string($params)) {
        
// Load the DB config file if a DSN string wasn't passed
        
if (strpos($params, '://') === FALSE)
        
{
            
include(APPPATH.'config/database'.EXT);
            
            if ( ! isset(
$db) OR count($db) == 0)
            
{
                show_error
('No database connection settings were found in the database config file.');
            
}
            
            
if ($params != '')
            
{
                $active_group
= $params;
            
}
            
            
if ( ! isset($active_group) OR ! isset($db[$active_group]))
            
{
                show_error
('You have specified an invalid database connection group.');
            
}
            
            $params
= $db[$active_group];            
        
}
        
else
        
{
            
            
/* parse the URL from the DSN string
            *  Database settings can be passed as discreet
             *  parameters or as a data source name in the first
             *  parameter. DSNs must have this prototype:
             *  $dsn = 'driver://username:password@hostname/database';
            */
        
            
if (($dns = @parse_url($params)) === FALSE)
            
{
                show_error
('Invalid DB Connection String');
            
}
            
            $params
= array(
                                
'dbdriver'    => $dns['scheme'],
                                
'hostname'    => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
                                
'username'    => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
                                
'password'    => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
                                
'database'    => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
                            
);
        
}
    }
else if (is_array($params)) {
        
// JUST DO NOTHING
    
}
    
    
// No DB specified yet?  Beat them senseless...
    
if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
    
{
        show_error
('You have not selected a database type to connect to.');
    
}

    
// Load the DB classes.  Note: Since the active record class is optional
    // we need to dynamically create a class that extends proper parent class
    // based on whether we're using the active record class or not.
    // Kudos to Paul for discovering this clever use of eval()
    
    
if ($active_record_override == TRUE)
    
{
        $active_record
= TRUE;
    
}
    
    
require_once(BASEPATH.'database/DB_driver'.EXT);

    if (! isset(
$active_record) OR $active_record == TRUE)
    
{
        
require_once(BASEPATH.'database/DB_active_rec'.EXT);
        
        if ( !
class_exists('CI_DB'))
        
{
            eval(
'class CI_DB extends CI_DB_active_record { }');
        
}
    }
    
else
    
{
        
if ( ! class_exists('CI_DB'))
        
{
            eval(
'class CI_DB extends CI_DB_driver { }');
        
}
    }
            
    
require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);

    
// Instantiate the DB adapter
    
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    
$DB =& new $driver($params);
    
    if (
$DB->autoinit == TRUE)
    
{
        $DB
->initialize();
    
}
    
    
return $DB;
}

Profile
 
 
Posted: 05 May 2008 08:48 AM   [ Ignore ]   [ # 2 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  14103
Joined  2002-06-03

Thanks for reporting, bbiao, but this was addressed in the SVN a few months ago. wink You can apply your fix to 1.6.1 or checkout the latest revision for the “official” fix.

 Signature 
Profile
 
 
Posted: 05 May 2008 08:16 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  7
Joined  2008-01-28
Derek Jones - 05 May 2008 08:48 AM

Thanks for reporting, bbiao, but this was addressed in the SVN a few months ago. wink You can apply your fix to 1.6.1 or checkout the latest revision for the “official” fix.

it’s cool

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 58779 Total Logged-in Users: 12
Total Topics: 69462 Total Anonymous Users: 0
Total Replies: 373915 Total Guests: 231
Total Posts: 443377    
Members ( View Memberlist )