提取一種對(duì)數(shù)據(jù)庫(kù)配置的通用方式
目的是通過(guò)通用類訪問(wèn)配置文件的方式,提供對(duì)數(shù)據(jù)庫(kù)連接的動(dòng)態(tài)獲取和設(shè)置,使開(kāi)發(fā)時(shí)和生產(chǎn)應(yīng)用時(shí)都能夠提供靈活的、簡(jiǎn)化的、解耦的操作方式。比如在配置文件中配置好兩套數(shù)據(jù)庫(kù)訪問(wèn)內(nèi)容,一套測(cè)試庫(kù)訪問(wèn)地址,一套生產(chǎn)庫(kù)訪問(wèn),在需要獲取連接信息時(shí),只要填入符合的參數(shù)即可
結(jié)構(gòu)
使用方式
db-config.json
,內(nèi)容為數(shù)據(jù)庫(kù)連接信息;db-config.php
提供公共靜態(tài)訪問(wèn),供上層接口調(diào)用;見(jiàn)代碼
{ "debug": { "db_host": "", "db_name": "", "db_user": "", "db_password": "" } }
php類
?php class DbConf{ public static function Conf( $conf_name ){ if(empty($conf_name)){ die("Illegal parameter"); } $from = "localhost"; // allow legal host only if(!isset($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST']!=$from){ die("Unauthorized access"); } $json_config = file_get_contents('db-config.json'); $json_data = json_decode($json_config, true); if( array_key_exists($conf_name, $json_data)){ return $json_data[$conf_name]; }else{ return "Not Found"; } } }
這篇文章就介紹到這了,需要的朋友可以參考一下
標(biāo)簽:威海 巴中 張家口 林芝 泰州 梅州 成都 山東
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP 訪問(wèn)數(shù)據(jù)庫(kù)配置通用方法(json)》,本文關(guān)鍵詞 PHP,訪問(wèn),數(shù)據(jù)庫(kù),配置,通用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。