Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Sunday, January 28, 2018

Database in codeigniter !!!!

For using the database in codeigniter, first of all you need to connect your database using the user name, password and host name. In core php, you need a connection of database every time when you want to run database queries, but in codeigniter it is not like that. You have to configure your database for only one time. And it will load automatically every time you run your project. This is done only by loading the database library of codeigniter.

Codeigniter provides the functionality of autoload. The libraries that we want to load frequently in our project, so instead of loading them each time, we will load them in the autoload.php file which resides in the ../application/config/autoload.php in codeigniter.

Database library is the built in library which comes in the codeigniter package. we just need to load it and configure our database.

How to configure our database?

In order to configure our database, go to application folder, then config folder, then click on the database.php file and open it.
At the end of this file you will find a code some thing like this:
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',          //here you have to give your host name
'username' => 'root',                  //here you have to give the username
'password' => '',                         //here you have to give the password. In case of                                                                         localhost, it is null

'database' => 'yourDatabaseName',          //your database name comes here
'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
);

After giving the user name, password and host name. Save it , and your database will be configured and ready to use. Now for using your database and running queries of database you just need to load your database library.

How to load a database library?

There are two ways to do it. One way is to load your database library manually each time when you want to run queries. 

<?php
class MyModel extends CI_Model{

public function fetchingData(){

$this->load->database();

}
}
?>

Now by loading the database library, the db object becomes available, and we can use different operations of database like insert, delete, update and select etc through this object like this:

<?php
class MyModel extends CI_Model{

public function fetchingData(){

$this->load->database();          //loading library manually each time
$this->db->select('myTable');
$this->db->get();

}
public function deleteTable(){

$this->load->database();          //loading library manually each time
$this->db->delete('myTable');
$this->db->get();

}
}
?>

Other way is to load your library in to the autoload.php file for the one time only. It will load automatically every time your application starts. Go to the application folder then to the config folder and then open the autoload.php file. complete path is ../application/config/autoload.php. Their you will find an array of libraries that you want to load automatically. It will be empty because you have not loaded any libraries in autoload array yet. Just load the database library their. like this:

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

After loading the library in that array, save the file and close it. Now you don't need to load the database library again. Just use it.

I hope this post was help for you all, leave your queries in the comments, if any ! God bless you all.