在php代碼中創(chuàng)建數(shù)據(jù)庫配置文件。(推薦學習:PHP編程從入門到精通)
將數(shù)據(jù)庫用戶名密碼等信息寫到配置文件config.php里,在需要引用配置文件內(nèi)容的文件里用include或require包含進來,并在函數(shù)里用global關鍵字將存放數(shù)據(jù)庫名,密碼等的變量全局化,這樣就可以在文件里的函數(shù)里使用了。
1,config.php 數(shù)據(jù)庫配置文件:
<?php $db_name="test"; $db_username="root"; global $db_password; ?>
數(shù)據(jù)庫操作類(調(diào)用配置文件)db.fun.php:
<?php require("config/config.php"); class db{ function fun(){ global $db_username,$db_password; echo "數(shù)據(jù)庫用戶名:".$db_username."<br />"; echo "數(shù)據(jù)庫密碼:".$db_password."<br />"; } } ?>
應用文件test.php:
<?php require("include/db.fun.php"); $a= new db(); $a->fun(); ?>