Welcome Guest, Not a member yet? Register   Sign In
New in CI, DB connection problem
#1

[eluser]emateu[/eluser]
Hi all. I'm new with codeigniter.

I have the following problem trying to connect to a db.

I have method called getTable() in a model called "Test_model" that returns $this->db->get('tablename')->result().

Then, in the controller, I'm doing the following:
Code:
$this->load->model('Test_model');
var_dump($this->Test_model->getTable());

Also I configureted the database in config/database.php

Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'mysql';
$db['default']['database'] = 'test_table';
$db['default']['dbdriver'] = 'mysql';
$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;

And setted in autoload.php the following line: $autoload['libraries'] = array('database');

And didn't work. I get only blank screen without errors.

Also y tryed the following:

Removed the $autoload['libraries'] = array('database'); leaving it like default ($autoload['libraries'] = array()) and called like this in the controller:

Code:
$this->load->model('Test_model');
$this->load->database();
var_dump($this->Test_model->getTable());

And I had the same problem.

But here doesn't finish. The curious think is if a do:

Code:
$db['hostname'] = 'localhost';
$db['username'] = 'root';
$db['password'] = 'mysql';
$db['database'] = 'test_table';
$db['dbdriver'] = 'mysql';
$db['dbprefix'] = '';
$db['pconnect'] = TRUE;
$db['db_debug'] = TRUE;
$db['cache_on'] = FALSE;
$db['cachedir'] = '';
$db['char_set'] = 'utf8';
$db['dbcollat'] = 'utf8_general_ci';
$db['swap_pre'] = '';
$db['autoinit'] = TRUE;
$db['stricton'] = FALSE;

$this->load->model('Test_model', '', $db);
var_dump($this->Test_model->getTable());

It works!

I'm doing something wrong? I dont understand what is happening.

Thanks
Emiliano

PD.: sorry for my english
#2

[eluser]allaerd[/eluser]
in youre file database.php where youre config is.... do you have:
Code:
$active_group = 'default';

If you do not have this change it and you will see that:
Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'mysql';
$db['default']['database'] = 'test_table';
$db['default']['dbdriver'] = 'mysql';
$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;

would work!
#3

[eluser]allaerd[/eluser]
and i also think the option:

Code:
$db['default']['autoinit'] = TRUE;

loads it by default. (not very sure)
#4

[eluser]emateu[/eluser]
I have the line

Code:
$active_group = 'default';

and

Code:
$db['default']['autoinit'] = TRUE;

in database.php

Thanks for your help
#5

[eluser]danmontgomery[/eluser]
Have you checked the apache error log? Enabled CI's logging and checked that log?




Theme © iAndrew 2016 - Forum software by © MyBB