Connecting Your CodeIgniter Application to the Database : Day 4

Connecting Your CodeIgniter Application to the Database

Welcome back to Day 4 of our journey to build a portfolio website with Connecting Your CodeIgniter Application to the Database! Now that we’ve set up our database, it’s time to connect our CodeIgniter application to it. This step is crucial as it allows our application to retrieve and manipulate data stored in the database. Let’s get started!

Connecting Your CodeIgniter Application to the Database

Connecting CodeIgniter to MySQL Database

CodeIgniter makes database connectivity straightforward using its database configuration settings. Follow these steps to connect your application to the MySQL database we set up earlier:

Step 1: Configure Database Settings

  1. Open database.php Configuration File: Navigate to application/config/database.php.
  2. Update Database Configuration: Modify the $db['default'] array to match your MySQL database settings:
$db['default'] = array(
    'dsn'      => '',
    'hostname' => 'localhost', // or your database host
    'username' => 'root',      // your database username
    'password' => '',          // your database password
    'database' => 'portfolio_website', // your database name
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Save the Configuration File: Ensure you save the changes made to database.php.

Step 2: Auto-Load Database Library (Optional)

You can configure CodeIgniter to automatically load the database library so that it’s available globally throughout your application. Open application/config/autoload.php and add 'database' to the $autoload['libraries'] array:

$autoload['libraries'] = array('database');

Step 3: Test Database Connection

To verify that CodeIgniter can connect to your database:

  1. Create a Test Controller: Navigate to application/controllers/ and create a new file named Test_db.php.
  2. Add Test Code:
<?php
class Test_db extends CI_Controller {
    public function index() {
        $this->load->database();
        if ($this->db->conn_id) {
            echo "Database connected successfully!";
        } else {
            echo "Database connection failed!";
        }
    }
}

Access the Controller: Open your web browser and go to http://localhost/your_project_name/index.php/test_db. You should see the message “Database connected successfully!“.

Performing Basic Database Operations

Now that we’re connected to the database, let’s perform some basic CRUD (Create, Read, Update, Delete) operations. In Day 5, we’ll dive into creating models and controllers to handle these operations for our portfolio website.

Compalgo Labs offering School Management Software : Setting Up the Database

Conclusion

Congratulations! You’ve successfully connected your CodeIgniter application to the MySQL database. This foundational step will enable us to build dynamic and interactive features for our portfolio website.

Stay tuned for Day 5, where we’ll start creating models and controllers to interact with our database. Feel free to leave any questions or comments below. Happy coding!


About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *