Welcome Guest, Not a member yet? Register   Sign In
Retrieving database parameter for connection
#1

[eluser]abmcr[/eluser]
I want to get in an array or in a string the data for connecting at the database stored in config/database.php
I have tried with
Code:
$a=$this->config->load("database",TRUE);
print_r($a);

but i get an error message....
#2

[eluser]Craig A Rodway[/eluser]
What error message... ?
#3

[eluser]abmcr[/eluser]
An Error Was Encountered

Your database.php file does not appear to contain a valid configuration array.
#4

[eluser]Craig A Rodway[/eluser]
Could you post the contents of your config/database.php file (obviously changing usernames/passwords first)?
#5

[eluser]abmcr[/eluser]
Code:
....

$active_group = "default";

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "comune";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['active_r'] = TRUE;
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = $_SERVER["DOCUMENT_ROOT"]."/cachedb/";


$db['hosting']['hostname'] = "localhost:8889";
$db['hosting']['username'] = "root";
$db['hosting']['password'] = "root";
$db['hosting']['database'] = "rapyd";
$db['hosting']['dbdriver'] = "mysql";
$db['hosting']['dbprefix'] = "";
$db['hosting']['active_r'] = TRUE;
$db['hosting']['pconnect'] = TRUE;
$db['hosting']['db_debug'] = TRUE;
$db['hosting']['cache_on'] = FALSE;
$db['hosting']['cachedir'] = "";

?>
#6

[eluser]Craig A Rodway[/eluser]
That file looks OK. I think you are trying to access the config settings incorrectly. The documentation does not mention any return values for $this->config->load(). To access the data yo need to use $this->config->item() - More info.
#7

[eluser]abmcr[/eluser]
I have tried with
Code:
echo $this->onfig->name("username");
but i get the same error :-(
#8

[eluser]xwero[/eluser]
The database config is not a normal config file and the database class is not set up to extract the values from the config file so you could add following method to the MY_Config class
Code:
function dbitem($str = null,$group= 'default')
{
   if(is_null($str))
   {
     return false;
   }
  
   include(APPPATH.'config/database'.EXT);

   if(!array_key_exists($str,$db[$group]))
   {
     return false;
   }
  
   return $db['group'][$str];
}
And then you can do
Code:
$this->config->dbitem('username');
#9

[eluser]abmcr[/eluser]
Thank you: all work fine.
the correct function is
Code:
function dbitem($str = null,$group= 'default')
{
   if(is_null($str))
   {
     return false;
   }
  
   include(APPPATH.'config/database'.EXT);

   if(!array_key_exists($str,$db[$group]))
   {
     return false;
   }
  
   return $db[$group][$str];
}
For getting value
Code:
echo $this->dbitem('username');

Ciao !!!




Theme © iAndrew 2016 - Forum software by © MyBB