CodeIgniter Forums
Retrieving database parameter for connection - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Retrieving database parameter for connection (/showthread.php?tid=5112)



Retrieving database parameter for connection - El Forum - 01-07-2008

[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....


Retrieving database parameter for connection - El Forum - 01-07-2008

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


Retrieving database parameter for connection - El Forum - 01-07-2008

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

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


Retrieving database parameter for connection - El Forum - 01-07-2008

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


Retrieving database parameter for connection - El Forum - 01-07-2008

[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'] = "";

?>



Retrieving database parameter for connection - El Forum - 01-07-2008

[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.


Retrieving database parameter for connection - El Forum - 01-07-2008

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


Retrieving database parameter for connection - El Forum - 01-07-2008

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



Retrieving database parameter for connection - El Forum - 01-07-2008

[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 !!!