How to connect to MySQL database in CodeIgniter?

Yesterday someone asked me to make a tutorial about Connecting to Database in CodeIgniter Framework. In order to make connection to database in Ci , we need to do only few configuration changes in your database.php config file which is located at application / config / database.php.

After making changes to database.php config file, we can connect to the database in two ways.
First one is Automatic loading, to do this just go to autoload.php file which is located at application/ config / database.php file. Then add ‘database’ to $autoload[‘libraries’] = array() array.

Another way of loading is manual loading with $this->load->database() method. place this line to load database in your controller contractor or with in the method(s).

Sometimes you may get fallowing error i got this error message with net4india shared hosting environment

In order to fix this issue , i simple changed config values of pconnect to false.

That’s it for database connecting and loading in CI, if you have any issues , let me know via comments.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

16 Comments

  1. why its’ not does not work for me?

    Any other Solution.. for :

    A Database Error Occurred
    Unable to connect to your database server using the provided settings.
    Filename: core/Loader.php
    Line Number: 346

  2. Hi, I got the same problem as well :
    Unable to connect to your database server using the provided settings.
    Filename: core/Loader.php
    Line Number: 347
    I’m working on Mac OSX Yosemite. Can you help? THX

  3. Thanks,

    But now i got the below error

    A PHP Error was encountered

    Severity: 8192

    Message: mysql_escape_string(): This function is deprecated; use mysql_real_escape_string() instead.

    Filename: mysql/mysql_driver.php

    Line Number: 319

  4. hi Arjun i have a one problem please help me i want to set session in laravel framework and get in other framework like codeigniter is it possible or not pls help me.

  5. Hi i have an error

    Fatal error: Call to a member function result() on boolean on line 774,

    I have two database, im trying to get all the data from other database

    heres my error line : return $result = $query->result();

    what does it mean?

    Pls help me im stuck in this problem for 1day already, i also searched in google the error, and i cant find a clear answer, help me pls

  6. Hi Arjun i have a xampp installed on port 80 on which mysql is installed and i am also using eclipse i have created a project which uses php builtin server which is running on port 81 how should i configure codeigniter so my project which is running on php builtin server can connect mysql which is running on xampp

  7. Hi, Thanks for sharing this information.

    I am currently trying to deploy Codeignitor application on Amazon AWS and facing the following error:

    Front-end:
    “Unable to connect to your database server using the provided settings.
    Filename: third_party/MX/Base.php
    Line Number: 59”
    ————————————————–
    Log viewer:
    – “mysqli::real_connect(): (HY000/1049): Unknown database ‘ ‘ /var/app/current/system/database/drivers/mysqli/mysqli_driver.php 201”
    – “Unable to connect to the database”

    The elastic beanstalk works well and I can connect successfully from MySQL Workbench.

Leave a Reply

Your email address will not be published.