load database in codeigniter

CodeIgniter: Load Database

To load a database in CodeIgniter, follow these steps:

Step 1: Open the config/database.php file in your CodeIgniter project. This file contains the database settings.

Step 2: Look for the $db['default'] array in the database.php file. This array contains the configuration settings for the default database connection.

Step 3: Update the configuration settings in the $db['default'] array according to your database setup. The important settings to configure are:

  • 'hostname': The hostname or IP address of your database server.
  • 'username': The username to access your database.
  • 'password': The password for the database user.
  • 'database': The name of the database you want to connect to.
  • 'dbdriver': The database driver to use (e.g., 'mysqli', 'pdo', 'postgre', etc.).
  • 'dbprefix': An optional table prefix to use for your tables.
  • 'pconnect': Set this to TRUE if you want to use a persistent database connection, or FALSE otherwise.
  • 'db_debug': Set this to TRUE if you want to display database errors, or FALSE otherwise.
  • 'cache_on': Set this to TRUE if you want to enable query caching, or FALSE otherwise.

Step 4: Save the database.php file with the updated configuration settings.

Step 5: In your CodeIgniter controller or model, you can now load the database library using the following code:

$this->load->database();

This will initialize the database connection using the configuration settings specified in the database.php file.

Step 6: Once the database is loaded, you can use the CodeIgniter database library to perform various database operations such as querying, inserting, updating, and deleting data.

That's it! You have successfully loaded the database in CodeIgniter. Now you can start using the database library to interact with your database.