Database Configuration


The default config file is located in MODPATH/database/config/database.php. You should copy this file to APPPATH/config/database.php and make changes there, in keeping with the cascading filesystem.
The database configuration file contains an array of configuration groups. The structure of each database configuration group, called an "instance", looks like this:
string INSTANCE_NAME => array(
    'type'         => string DATABASE_TYPE,
    'connection'   => array CONNECTION_ARRAY,
    'table_prefix' => string TABLE_PREFIX,
    'charset'      => string CHARACTER_SET,
    'profiling'    => boolean QUERY_PROFILING,
),
Understanding each of these settings is important.


INSTANCE_NAME
Connections can be named anything you want, but you should always have at least one connection called "default".
DATABASE_TYPE
One of the installed database drivers. Kohana comes with "mysql" and "pdo" drivers. Drivers must extend the Database class.
CONNECTION_ARRAY
Specific driver options for connecting to your database. (Driver options are explained below.)
TABLE_PREFIX
Prefix that will be added to all table names by the query builder. Prepared statements will not use the table prefix.
QUERY_PROFILING
Enables profiling of database queries. This is useful for seeing how many queries each page is using, and which are taking the longest. You must enable the profiler the view these stats.

Example

The example file below shows 2 MySQL connections, one local and one remote.
return array
(
    'default' => array
    (
        'type'       => 'mysql',
        'connection' => array(
            'hostname'   => 'localhost',
            'username'   => 'dbuser',
            'password'   => 'mypassword',
            'persistent' => FALSE,
            'database'   => 'my_db_name',
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'profiling'    => TRUE,
    ),
    'remote' => array(
        'type'       => 'mysql',
        'connection' => array(
            'hostname'   => '55.55.55.55',
            'username'   => 'remote_user',
            'password'   => 'mypassword',
            'persistent' => FALSE,
            'database'   => 'my_remote_db_name',
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'profiling'    => TRUE,
    ),
);

Connections and Instances

Each configuration group is referred to as a database instance. Each instance can be accessed by calling Database::instance. If you don't provide a parameter, the default instance is used.
// This would connect to the database defined as 'default'
$default = Database::instance();
 
// This would connect to the database defined as 'remote'
$remote  = Database::instance('remote');
To disconnect the database, simply destroy the object:
unset($default)
 
// Or
 
unset(Database::$instances['default']);
If you want to disconnect all of the database instances at once:
Database::$instances = array();

Connection Settings

Every database driver has different connection settings.

MySQL

MySQL database can accept the following options in the connection array:
TypeOptionDescriptionDefault value
stringhostnameHostname of the databaselocalhost
integerportPort numberNULL
stringsocketUNIX socketNULL
stringusernameDatabase usernameNULL
stringpasswordDatabase passwordNULL
booleanpersistentPersistent connectionsFALSE
stringdatabaseDatabase namekohana

PDO

PDO database can accept these options in the connection array:
TypeOptionDescriptionDefault value
stringdsnPDO data source identifierlocalhost
stringusernameDatabase usernameNULL
stringpasswordDatabase passwordNULL
booleanpersistentPersistent connectionsFALSE
If you are using PDO and are not sure what to use for the dsn option, review PDO::__construct.

1 comments:

Blogger said...

Bluehost is ultimately the best hosting company for any hosting services you might need.

Post a Comment