How to connect database in Codeigniter?

by cortez.connelly , in category: PHP Frameworks , 2 years ago

How to connect database in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by aubrey , 2 years ago

@cortez.connelly 

To connect to a database in Codeigniter, you will need to first configure your database settings in the application/config/database.php file.


You can do this by modifying the values in the $db['default'] array, which should contain the following keys:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password',
    'database' => 'your_database',
    '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
);


Once you have configured your database settings, you can use the Codeigniter database library to connect to your database. To do this, you will need to load the database library in your controller using the following line of code:

1
$this->load->database();


You can then use the database library to perform various database operations, such as executing queries or inserting data into the database.


For example, to execute a SELECT query, you can use the $this->db->get() method:

1
$query = $this->db->get('your_table');


This will return a result object containing the results of the query. You can then use the result object to access the returned data.

1
2
3
4
5
6
foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}


For more information on using the Codeigniter database library, you can refer to the Codeigniter documentation at https://codeigniter.com/user_guide/database/index.html.

Member

by lizzie , a year ago

@cortez.connelly 

To connect a database in CodeIgniter, you need to follow these steps:

  1. Open the application/config/database.php file.
  2. Define your database connection details in the 'db' array. Here is an example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
    'dsn'      => '',
    'hostname' => 'localhost', // your database server
    'username' => 'myuser', // your database username
    'password' => 'mypassword', // your database password
    'database' => 'mydatabase', // your database name
    'dbdriver' => 'mysqli', // database driver, such as mysqli, pdo, etc.
    '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
);


  1. Replace myuser, mypassword, and mydatabase with your actual database username, password, and database name.
  2. In your controller or model, you can now load the database library using the $this->load->database function. Here is an example:
1
2
3
4
5
6
class MyModel extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database(); // connect to the database
    }
}


  1. Now you can perform database operations using CodeIgniter's database functions, such as $this->db->query(), $this->db->get(), $this->db->insert(), etc.


Note: Make sure you have the correct database driver installed and enabled in your PHP configuration.