In this tutorial, we are going to learn CodeIgniter Database Pattern. It will help us to process database operation in CodeIgniter. We have already developed a connection in the previous CodeIgniter database connection tutorial. So now we will understand these database patterns.

For the General information, we cannot create the instance of database programmatically from CodeIgniter. We have to manually create the database from “phpmyadmin”. Once you will create the instance of the database you can place that in the connection.

Codeigniter provide as two different syntaxes to perform database operations.

  • Advanced Conventional Query Method
  • Active Records

Advanced Conventional Query Method

This is the advanced but conventional way to communicate with the database. In simple words, it will provide as a “query()” method in which we will write the queries to execute. This method is very efficient because we don’t need to connect the database to execute the queries and after the execution, we don’t need to close the connection it will happen automatically.

This method is useful for MySQLi Geeks. You can create extensive queries and also create joints according to the logic which you want to build to extract the records.

In the following example, you can observe how we have written the query with the help of this method.

$this->db->query("Insert Into User (Name, Age) Values ('TutorialPace.com', 22)");

Active Records

This is a shorthand code and rich-featured methods to process database operations within CodeIgniter. Mostly developer uses this feature when they need to execute small queries. Such as to fetch the records from the database and to insert records in the specific table. In the following code, you can observe how we can fetch all the record from a Table.

$this->db->get('tutorialpace_user');
// Produces: SELECT * FROM tutorialpace_user

Basically, it is an efficient pattern design to optimize the line of code to process database operations. Behind the scene, each active pattern has an adapter which is already written by CodeIgniter to perform the database operation. The best thing is that even a single line of code can process a longer query.

Both methodologies are efficient to operate database operations. When there are a lot of complex data to be processed developer recommended using conventional methodology by developing your own MySQLi scripts.