phpmysqlcodeigniter

CodeIgniter - Database Error while connecting from Localhost


I'm fairly new to development and I'm completely new to codeigniter.

I've downloaded a CodeIgniter project from my company's server that I inherited from a previous developer.

I need to add a new page to the site. Since I'm new to CI, I (obviously) don't want to edit the live site but I can't get the site to run locally. I'm trying to run it on a local server either through MAMP or through the PHP server in terminal on a Mac running Yosemite 10.10.5.

I've added the project folder to htdocs in MAMP but when I open http://localhost:8888/index.php/contact the screen is blank. When I try to access the same folder through the terminal using:

env DB_USER=XXXX DB_PASSWD=XXXX DB_NAME=XXXX php -S localhost:8080

(where 'XXXX' is replaced with our database username and password)

I get the following error:

Unable to connect to your database server using the provided settings.

Filename: core/Loader.php

Line Number: 346

Line 346 of core/Loader.php is the public function database in the attached image.

Things I've tried:

• I've tried changing the $config['base_url'] to 'http://localhost:8888/'; but I still get the same error

• Deleting the hostname, username and password from the database.php file, still get the same error

I'm not sure why a local version can't access a database when the live site can with the same login credentials? Any help is much appreciated as I just started this new position, this is one of my first projects for the company and it has me stumped. I still need to figure out how to add a page, but I can't even get the site loaded locally so I can work on it.

Update

Here's what my database.php file looks like.....it's not setup as an array:

/* The $active_group variable lets you choose which connection group to make active. By default there is only one group (the 'default' group). The $active_record variables lets you determine whether or not to load the active record class */

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'database_name'; // contains actual name
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

** Link to video of me (trying to) explain this issue: **

Link #1: CodeIgniter Database Error Issue YouTube

Thanks for your help!

core/loader.php


Solution

  • If you are running it MAMP ( Mac version) Your mysql default password is "root"

    So database config file will look like

    $active_group = 'default';
    $active_record = TRUE;
    
    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = 'root';
    $db['default']['database'] = 'database_name'; // contains actual name
    $db['default']['dbdriver'] = 'mysqli';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;
    

    Updated $db['default']['password'] = 'root';