Active Record and database queries - Codelgniter Tutorial

Welcome to this comprehensive tutorial on using Active Record for performing database queries in CodeIgniter. Active Record is a powerful feature of CodeIgniter that simplifies the process of interacting with databases. In this tutorial, we will cover the steps involved in using Active Record to perform CRUD (Create, Read, Update, Delete) operations in CodeIgniter applications.

Introduction to Active Record in CodeIgniter

Active Record is a database abstraction layer provided by CodeIgniter. It allows you to perform database operations using a simple and intuitive syntax, without the need to write raw SQL queries. Active Record provides a convenient and secure way to interact with databases, ensuring data consistency and security.

Using Active Record for Database Queries

To use Active Record for database queries in CodeIgniter, follow these steps:

Step 1: Load the Database Library

Before using Active Record, make sure to load the CodeIgniter database library in your controller or component. You can load the database library using the $this->load->database() method. Here's an example:


$this->load->database();

By default, CodeIgniter uses the configuration settings from application/config/database.php to establish a database connection.

Step 2: Perform Database Queries

Once the database library is loaded, you can use Active Record methods to perform database queries. Here are examples of common CRUD operations:

  • Create: To insert data into the database, you can use the $this->db->insert() method. Here's an example:

$data = array(
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 30
);

$this->db->insert('users', $data);
  • Read: To retrieve data from the database, you can use methods like $this->db->get() or $this->db->get_where(). Here's an example:

$query = $this->db->get('users'); // Retrieves all users

$query = $this->db->get_where('users', array('id' => 1)); // Retrieves a specific user
  • Update: To update data in the database, you can use the $this->db->update() method. Here's an example:

$data = array(
    'name' => 'Jane Smith',
    'email' => 'jane@example.com',
    'age' => 28
);

$this->db->where('id', 1);
$this->db->update('users', $data);
  • Delete: To delete data from the database, you can use the $this->db->delete() method. Here's an example:

$this->db->where('id', 1);
$this->db->delete('users');

Common Mistakes to Avoid

  • Not loading the database library before using Active Record methods.
  • Incorrectly specifying the table name or column names in Active Record methods.
  • Not using appropriate security measures like parameter binding or input sanitization.

Frequently Asked Questions (FAQs)

  1. Can I use Active Record with multiple database connections?

    Yes, you can use Active Record with multiple database connections in CodeIgniter. By specifying the database group in the Active Record methods, you can perform queries on different databases within the same application.

  2. Can I perform complex queries using Active Record?

    Yes, you can perform complex queries using Active Record in CodeIgniter. Active Record provides various methods and query builder features to handle complex queries, including joins, subqueries, and conditional statements.

  3. How can I protect against SQL injection when using Active Record?

    Active Record in CodeIgniter includes security features to protect against SQL injection. It automatically escapes input values to prevent malicious SQL injection attacks. However, it is still recommended to use parameter binding or query bindings for added security.

  4. Can I use Active Record for non-CRUD operations?

    Yes, you can use Active Record for non-CRUD operations. Active Record provides additional methods and functionalities for handling other types of database operations, such as counting records, selecting specific columns, or setting custom query conditions.

  5. Can I use raw SQL queries with Active Record?

    Yes, you can use raw SQL queries with Active Record. CodeIgniter's Active Record supports the use of raw SQL queries through the $this->db->query() method. However, it is recommended to use Active Record methods whenever possible to maintain the benefits of database abstraction and security.

Summary

In this tutorial, we explored how to use Active Record for performing database queries in CodeIgniter. Active Record simplifies the process of interacting with databases by providing an intuitive and secure syntax. By following the steps to load the database library and use Active Record methods, you can perform CRUD operations efficiently and securely in your CodeIgniter applications.