CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at app/Config/Database.php. You can also set database connection values in the .env file. See below for more details. The config settings are stored in
a class property that is an array with this prototype: The name of the class property is the connection name, and can be used while connecting to specify a group name. Note The default location of the SQLite3 database is in the DSNSome database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might require a full DSN string to be provided. If that is the case, you should use the ‘DSN’ configuration setting, as if you’re using the driver’s underlying native PHP extension, like this: <?php // PDO $default = [ 'DSN' => 'pgsql:host=localhost;port=5432;dbname=database_name', // ... ]; // Oracle $default = [ 'DSN' => '//localhost/XE', // ... ]; Note If you do not specify a DSN string for a driver that requires it, CodeIgniter will try to build it with the rest of the provided settings. You can also set a Data Source Name in universal manner (URL like). In that case DSNs must have this prototype: <?php $default = [ 'DSN' => 'DBDriver://username:[email protected]:port/database', // ... ]; To override default config values when connecting with a universal version of the DSN string, add the config variables as a query string: <?php // MySQLi $default = [ 'DSN' => 'MySQLi://username:[email protected]:3306/database?charset=utf8&DBCollat=utf8_general_ci', // ... ]; // Postgre $default = [ 'DSN' => 'Postgre://username:[email protected]:5432/database?charset=utf8&connect_timeout=5&sslmode=1', // ... ]; Note If you provide a DSN string and it is missing some valid settings (e.g., the database character set), which are present in the rest of the configuration fields, CodeIgniter will append them. FailoversYou can also specify failovers for the situation when the main connection cannot connect for some reason. These failovers can be specified by setting the failover for a connection like this: <?php $default['failover'] = [ [ 'hostname' => 'localhost1', 'username' => '', 'password' => '', 'database' => '', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => true, 'DBDebug' => true, 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, ], [ 'hostname' => 'localhost2', 'username' => '', 'password' => '', 'database' => '', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => true, 'DBDebug' => true, 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, ], ]; You can specify as many failovers as you like. You may optionally store multiple sets of connection values. If, for example, you run multiple environments (development, production, test, etc.) under a single installation, you can set up a connection group for each, then switch between groups as needed. For example, to set up a “test” environment you would do this: <?php namespace Config; use CodeIgniter\Database\Config; class Database extends Config { public $test = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'database_name', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => true, 'DBDebug' => true, 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'compress' => false, 'encrypt' => false, 'strictOn' => false, 'failover' => [], ]; // ... } Then, to globally tell the system to use that group you would set this variable located in the config file: <?php $defaultGroup = 'test'; Note The name ‘test’ is arbitrary. It can be anything you want. By default we’ve used the word “default” for the primary connection, but it too can be renamed to something more relevant to your project. defaultGroupYou could modify the config file to detect the environment and automatically update the defaultGroup value to the correct one by adding the required logic within the class’ constructor: <?php namespace Config; use CodeIgniter\Database\Config; class Database extends Config { public $development = [/* ... */]; public $test = [/* ... */]; public $production = [/* ... */]; public function __construct() { $this->defaultGroup = ENVIRONMENT; } } Configuring with .env FileYou can also save your configuration values within a .env file with the current server’s database settings. You only need to enter the values that change from
what is in the default group’s configuration settings. The values should be name following this format, where database.default.username = 'root'; database.default.password = ''; database.default.database = 'ci4'; Explanation of Values:
Note Depending on what database platform you are using (MySQL, PostgreSQL, etc.) not all values will be needed. For example, when using SQLite you will not need to supply a username or password, and the database name will be the path to your database file. The information above assumes you are using MySQL. What is configuration file in php?The PHP configuration file allows you to configure the modules enabled, the email settings or the size of the upload files. It is located at installdir/php/etc/php. ini. For example, to modify the default upload limit for PHP, update the PHP configuration file following these instructions.
Where is config DB php?The database configuration file is app/config/database. php . In this file you may define all of your database connections, as well as specify which connection should be used by default.
Where is database configuration file?The config file is located at app/Config/Database.
HOW include config file in php?php' file. Code 2: Accept the returning array in a variable. $details = include ( 'config. php' );. Code 1: Create a PHP file and save it with the name 'config. php'. ... . Code 2: Create a PHP file and save it with the name 'try. ... . Output:. |