mysql配置类,MySQL数据库配置在单独的类中

Is it possible to keep all my database related configuration (hostnames, usernames, passwords, and databases) as well as the function to connect to and select the correct database in a separate class?

I tried something like this:

class Database

{

var $config = array(

'username' => 'someuser',

'password' => 'somepassword',

'hostname' => 'some_remote_host',

'database' => 'a_database'

);

function __construct() {

$this->connect();

}

function connect() {

$db = $this->config;

$conn = mysql_connect($db['hostname'], $db['username'], $db['password']);

if(!$conn) {

die("Cannot connect to database server");

}

if(!mysql_select_db($db['database'])) {

die("Cannot select database");

}

}

}

And then in another class I would use in the classes __construct function:

require_once('database.php');

var $db_conn = new Database();

But this doesnt save the connection, it ends up defaulting to the servers local db connection. Or do I have to do the database commands everytime before I execute some database commands?

解决方案

I modified your class to work as you seem to be expecting it to:

class Database

{

var $conn = null;

var $config = array(

'username' => 'someuser',

'password' => 'somepassword',

'hostname' => 'some_remote_host',

'database' => 'a_database'

);

function __construct() {

$this->connect();

}

function connect() {

if (is_null($this->conn)) {

$db = $this->config;

$this->conn = mysql_connect($db['hostname'], $db['username'], $db['password']);

if(!$this->conn) {

die("Cannot connect to database server");

}

if(!mysql_select_db($db['database'])) {

die("Cannot select database");

}

}

return $this->conn;

}

}

Usage:

$db = new Database();

$conn = $db->connect();

Note that you can call connect() as many times as you like and it will use the current connection, or create one if it doesn't exist. This is a good thing.

Also, note that each time you instantiate a Database object (using new) you will be creating a new connection to the database. I suggest you look into implementing your Database class as a Singleton or storing it in a Registry for global access.

You can also do it the dirty way and shove it in $GLOBALS.

Edit

I took the liberty of modifying your class to implement the Singleton pattern, and follow the PHP5 OOP conventions.

class Database

{

protected static $_instance = null;

protected $_conn = null;

protected $_config = array(

'username' => 'someuser',

'password' => 'somepassword',

'hostname' => 'some_remote_host',

'database' => 'a_database'

);

protected function __construct() {

}

public static function getInstance()

{

if (null === self::$_instance) {

self::$_instance = new self();

}

return self::$_instance;

}

public function getConnection() {

if (is_null($this->_conn)) {

$db = $this->_config;

$this->_conn = mysql_connect($db['hostname'], $db['username'], $db['password']);

if(!$this->_conn) {

die("Cannot connect to database server");

}

if(!mysql_select_db($db['database'])) {

die("Cannot select database");

}

}

return $this->_conn;

}

public function query($query) {

$conn = $this->getConnection();

return mysql_query($query, $conn);

}

}

Usage:

$res = Database::getInstance()->query("SELECT * FROM foo;");

or

$db = Database::getInstance();

$db->query("UPDATE foo");

$db->query("DELETE FROM foo");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值