Monthly Archives: June 2013

CodeIgniter Database Connection Error

I have started learning how to use the PHP framework CodeIgniter. I’m not a professional coder, but I have dabbled with PHP and various other languages over the years.

As I’m new to the framework, the first order of business was to follow the tutorial to setup a basic application using CodeIgniter. I thought that this would be a fairly quick and easy thing to follow and then I could get on to playing about with it further. About 5 hours later and I finally got to the end of the tutorial with the sample app in working order. This post describes the first issue I came across and how I eventually resolved it.

The Problem

So, I had setup my database and configured username, password and database name correctly in the application/config/database.php file but was getting the following error when trying to access it from the CodeIgniter app.

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

I double-checked and triple-checked my database settings, and even setup a quick plain PHP test script to make sure I could connect to it and retrieve results successfully, which worked fine.

The Solution

After hours of reading posts from people with similar issues and trying various things, checking and re-checking all my settings etc., the following change fixed it for me.

In application/config/database.php change…

$db['default']['pconnect'] = TRUE;


$db['default']['pconnect'] = FALSE;

It seems to me that some web hosts maybe don’t like persistent database connections, and I guess my host is one of them!

Other Useful Information

Also worth mentioning here is a nice bit of code to add to the end of your application/config/database.php file to help troubleshooting database connection issues. In my case it actually connected without a problem (which at least confirmed that my connection settings were fine), but I read of others who found it very useful. Note that I didn’t write this myself, I found it on a few different posts from different people so not sure who to give credit to.

echo '<pre>';
echo '</pre>';

echo 'Connecting to database: ' .$db['default']['database'];
  or die('Cannot connect to the database because: ' . mysql_error());
  mysql_select_db ($db['default']['database']);

echo '<br /> Connected OK: '  ;
die( 'file: ' .__FILE__ . ' Line: ' .__LINE__);

Using this code you should be able to pinpoint where your connection issue lies.


Before I start posting random, potentially totally unrelated stuff here I guess I’d better give a brief introduction on why I created the site.


Quite often I have found that I spend a lot of time trying to figure out even the smallest problems that I come across while working, learning or even playing! Most of the time it’s something simple that is stopping me from carrying on whatever it is that I am trying to do.

I usually google these things hoping to find a quick solution but can end up spending hours sifting through different websites, forum posts and blogs before I eventually figure out how to fix whatever it is I am having trouble with.

These are the sorts of things I would like to post about here in the hopes that I can save other people that lost time and frustration by combining my experience with all the info I have found on that particular issue in one place.

I also tend to forget these things after a while even after all the time and effort spent to figure them out, so when I come across the same issue I have to work it all out again. So this site can serve as a sort of personal knowledgebase for myself at the very least.

Hopefully anything I write here can be of use to someone!


Another reason for the site is that I may post progress updates to do with any projects I might be working on. This could be web/programming stuff, or even gaming related. More than anything I hope posting updates might help keep me interested in whatever it is that I’m doing!