0

I am working on a codeigniter project. I developed it using wampserver locally, but I'd like to make some changes with xampp . It works fine on the remote site. Unfortunately when I try to run it locally I am getting:

A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: F:\xampp-portable\htdocs\....\database\DB_driver.php
Line Number: 124

to my index.php file , I have added:

mysql_connect("localhost","root","") or die ("Could not connect to mysql server.");
mysql_select_db("contacts_db") or die ("Could not connect to database.");

This does NOT generate an error. Can anyone give me some pointers on what to check next?

Thanks in advance,

Bill

OK guys, here is the code around line 124:

function initialize()
{
    // If an existing connection resource is available
    // there is no need to connect and select the database
    if (is_resource($this->conn_id) OR is_object($this->conn_id))
    {
        return TRUE;
    }

    // ----------------------------------------------------------------

    // Connect to the database and set the connection ID
    $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this-        >db_pconnect();

    // No connection resource?  Throw an error
    if ( ! $this->conn_id)
    {
        log_message('error', 'Unable to connect to the database'); // 124

        if ($this->db_debug)
        {
            $this->display_error('db_unable_to_connect');
        }
        return FALSE;
    }

John was right, although I am now confused. I had set up the following code in my index.php to auto switch between xampp and server database config files:

define('ENVIRONMENT', isset($_SERVER['SERVER_NAME'])=='my_domain_name.com' ? 'production' : 'development');

    echo 'SERVER_NAME  '.$_SERVER['SERVER_NAME']; // getting localhost
    echo 'env '.ENVIRONMENT; // getting production.

I thought this would result in the ENVIRONMENT constant set to 'development' with $_SERVER['SERVER_NAME']=localhost. Would someone mind explaining what I'm doing wrong here?

5
  • The error is coming out of DB_Driver.php - could you provide us with the code in that line number - i think the connection is established somewhere else but not in index.php. Commented Nov 29, 2012 at 22:31
  • add the code of your CI that outputs the error Commented Nov 29, 2012 at 22:49
  • 1
    uhhhhh have you set your db password and username in the config file or changed it since moving it to the new server? are the passwords really the same? I dont think its going to be localhost as the username for both... Commented Nov 30, 2012 at 0:07
  • Sort of skimming over the last update, try user/pass as root/root rather than root/'blank'. Xampp's default has the password set to root, where as mamp, wamp, etc I believe don't have passwords. Commented Nov 30, 2012 at 2:44
  • John please see my edits Commented Nov 30, 2012 at 2:44

1 Answer 1

3

Please check your config/database.php file, as I know the config database in codeigniter is in this file. Some values you need to pay attention are:

$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
Sign up to request clarification or add additional context in comments.

2 Comments

As the only answer, I am up voting you. You are correct as my environment settings code ( listed above ) was wrong and caused the wrong database config file to be selected in my local environment. I changed it to define('ENVIRONMENT', isset($_SERVER['SERVER_NAME'])=='my_domain_name.com' ? 'production' : 'development'); regards - Bill
Well I just thought that you might miss this settings since you create your own connection link to database and CI by default load its settings as well that might cause the conflict. However glad I could help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.