1

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

5
  • With Codeigniter I always run a duplicate copy on local host, then make any changes I need to, to the live site from that Commented Apr 12, 2017 at 3:59
  • thanks, I am trying to run in local host and getting database error as shown in video at link above Commented Apr 12, 2017 at 5:06
  • So I figured this out, thanks very much for all the help! I had (apparently) installed the mysql database in a different location and MAMP couldn't reference it. I realized this because the database I had imported was not showing up in MAMP > PHPMyAdmin. I had followed a Codeigniter tutorial which imported the DB from the terminal - then ran the internal PHP sever from the terminal php -S localhost:8080. This is what was causing the database error - there was NO DATABASE! MAMP couldn't see the DB that was uploaded to the terminal folder location. Commented Apr 13, 2017 at 4:05
  • The login configuration submitted by @Ankit for the database file in Ci application > config > database.php worked. The password needs to be set to root. Thanks again for all the help! Commented Apr 13, 2017 at 4:05
  • Additionally, when I tried to upload the DB into phpmyadmin it was too large, so I had to download it in two separate files from my server. Compressing both files into a zip file before uploading helped also. Commented Apr 13, 2017 at 4:07

3 Answers 3

2

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';

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Ankit, I tried this but no luck, I still get the error: A Database Error Occurred Unable to connect to your database server using the provided settings. Filename: core/Loader.php Line Number: 346
please disregard the reference to the database file. I had made a backup and placed it in my project folder.
2

The config file is located at application/config/database.php

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',  // pass this
        'username' => 'root',  // pass this
        'password' => '',  // pass this
        'database' => 'database_name',  // pass this
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);

Just pass the parameters in which I have mention // pass this and try again.

3 Comments

thanks, I've changed to these settings and still get the same error. I've downloaded the website database to my local computer and created a DB in MySQl with the same name, imported the .sql file into MySQL through terminal, so CodeIgniter should be trying to access that specific database. It's really frustrating, I can see the favicon for the site so I know it's loading some of the files.....
I added links to the OP to videos to hopefully explain / illustrate better what I'm working with and the error I'm getting. I have the database file inside the project folder......?
please ignore the reference to the database file. I had made a backup and placed it in my project folder.
1

Make sure that root has login permission in localhost. Some MySQL instances are very strict on these things.

2 Comments

thanks Matias, here's what I get when I enter SHOW GRANTS FOR 'root'@'localhost';
GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' WITH GRANT OPTION | | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION

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.