how to store database connection details?HOST,USERNAME,PASS
Moderator: General Moderators
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
how to store database connection details?HOST,USERNAME,PASS
I used to pass database connection details from the same file I am running the script.
Now, I think that its not a good idea.
I am looking for suggestions where to store database connection details...like in a file???
Now, I think that its not a good idea.
I am looking for suggestions where to store database connection details...like in a file???
Have a look at the registry pattern. Designed to store data in a globally accessible way.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
My situation may be different than most, but I will pass it on anyway in case someone can benefit from it. I have a number of databases (5-10) which I regularly connect to in various application. What I ended up doing was writing a parameterized Factory function into my database connection library. I happen to favor ADOdb as a database access layer, so by passing a string identifying which database I need to connect to, the Factory returns an ADOdb Connection already connected to the correct resource. If connection information changes (user names, hosts, etc.), I change them only in my standard db connection include and every application continues to work.
HTH
HTH
A lot of Model classes use an instance of the DAO class. The DAO class is a wrapper around ADODB (some extra SQL generation etc..) and accepts a DSN string and the table name in the constructor..
Because, i don't want all my Model classes to call new DAO(.., ...) for maintenance issues (dsn changes or dao is replaced by DAO2) i have a Configuration class... This class is responsible to generate the DAO that a Model class requires..
Here is how that Configuration class looks like...
And now implementations of a Model can use that as following:
Because, i don't want all my Model classes to call new DAO(.., ...) for maintenance issues (dsn changes or dao is replaced by DAO2) i have a Configuration class... This class is responsible to generate the DAO that a Model class requires..
Here is how that Configuration class looks like...
Code: Select all
/**
* This class represents a Configuration container
*
* @author Tim Van Wassenhove <timvw@users.sourceforge.net>
*/
class Configuration
{
/**
* Returns an appropriate DAO
*
* @param string $name the name of the DAO
* @return DAO an instance of the DAO
*/
function getDAO($name = '')
{
require_once(TVW_EXT . '/timvc/dao.php');
$dsn = 'mysql://user:password@example.com/dbname';
switch($name)
{
case 'messages':
$table = 'message';
$dao = new DAO($dsn, $table);
break;
case 'feeds':
case 'feedcontents':
default:
$table = $name;
$dao = new DAO($dsn, $table);
break;
}
return $dao;
}
}Code: Select all
class Users extends Model
{
function __construct()
{
$this->dao = Configuration::getDao('users');
parent::Model($dao);
}
}- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact: