Friday, November 22, 2024
Google search engine
HomeLanguagesDatabase Connection and Queries in Codeigniter

Database Connection and Queries in Codeigniter

To retrieve data from database we always need to connect our project with database. We will see how to connect database in codeignitor and how to run queries in it. In config folder we can find database.php file to connect with database. Mention the details as per shown image below.
database.php

In the above image, we need to define the username, password and database name. We can specify dbdriver like mysql or SQLite whatever we use, and that’s how it will be connected with our database and we will run our query. Create a database in localhost, define a table there and insert some dummy data. Now we need to load this database in model file and then we will be able to access the query.
Model:




<?php 
class Usermodel extends CI_Model {
        public function __construct(){
             parent::__construct();
             $this->load->database(); 
         }
      
        public function getUsers() {          
               $data = $this->db->query('SELECT * FROM city') ;
               print_r($data);
         }    
     
}
  
?>


From the above query the output will be display in object form.
Output:

Now use the same code above but this time use below function in model to see the output.

print_r($data->result());

Now the output will be something like below which is the array of objects.
Output:

Now use the same code above but this time use below function in model to see the output.

print_r($data->result_array());

This time output will be in array form.
Output:

All the above-mentioned points create confusion when we try to fetch data from our database. So we need to understand the difference between all the above three output results.
To get the array result in view all we need to do is to use foreach loop to access the data.

How to use Active Records: Active records are basically design patterns in CI which are used to deal with database queries. In the above all examples we wrote queries to fetch data, but inactive records we use built-in class or libraries and pass our own parameter to deal with the database. The benefit of using active records is code will be reduced and also if we need to deploy our project on another database like PostgreSQL or SQLite then we don’t need to change the queries in the model. From the above, all examples migrating the project to another database requires to change the query in the model which is time taking. Now let’s see how to use active records to deal with the database.

Model:




<?php 
class Usermodel extends CI_Model {
      public function __construct(){
      parent::__construct();
      $this->load->database(); 
  }
      
      public function getUsers() {    
      //SELECT * FROM `city` WHERE id = 1  
      $data = $this->db->select('city_name', 'state_name')
                       ->where('id', 1)
                       ->get('city');               
      print_r($data->result_array());
   }
         
}
?>


Output:

So we will get the same result as our previous example. There are so many active records queries available and can be used as per requirement. Please follow the link of Active Records to read about all the active records.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments