數(shù)據(jù)庫(kù)配置
CodeIgniter 有一個(gè)配置文件讓你存放數(shù)據(jù)庫(kù)連接值(username:用戶名,password:密碼,database name:數(shù)據(jù)庫(kù)名,等等..). The config file is located at application/config/database.php. You can also set database connection values for specific environments by placing database.php it the respective environment config folder.
配件文件存放在一個(gè)如下格式的一個(gè)多維數(shù)組里:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
我們使用多維數(shù)組的原因是為了讓你隨意的存儲(chǔ)多個(gè)連接值的設(shè)置。舉例:如果你運(yùn)行多個(gè)環(huán)境(development:開(kāi)發(fā)、production:制作、test:測(cè)試 等等..),你能為每個(gè)環(huán)境建立獨(dú)立的連接組,并在組直接進(jìn)行切換。舉例,設(shè)置一個(gè)"test"環(huán)境,你可以這樣做:
$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['password'] = "";
$db['test']['database'] = "database_name";
$db['test']['dbdriver'] = "mysql";
$db['test']['dbprefix'] = "";
$db['test']['pconnect'] = TRUE;
$db['test']['db_debug'] = FALSE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = "";
$db['test']['char_set'] = "utf8";
$db['test']['dbcollat'] = "utf8_general_ci";
$db['test']['swap_pre'] = "";
$db['test']['autoinit'] = TRUE;
$db['test']['stricton'] = FALSE;
那么,告訴系統(tǒng)使用"test"組,你可以設(shè)置位于配置文件中的變量:
$active_group = "test";
注意: "test"的名字是任意的,這可以讓你自由設(shè)置,我們的主要連接默認(rèn)使用"default"這個(gè)名字,當(dāng)然,您可以基于您的項(xiàng)目為它起一個(gè)更有意義的名字。
Active Record
Active Record 類(lèi) 可以通過(guò)數(shù)據(jù)庫(kù)配置文件里的$active_record變量進(jìn)行全局的設(shè)定(允許/禁止 TRUE/FALSE (boolean)). 如果你不用這個(gè)類(lèi),那么你可以通過(guò)將這個(gè)變量值設(shè)置成FALSE來(lái)減少在數(shù)據(jù)庫(kù)類(lèi)初始化時(shí)對(duì)電腦資源的消耗。
$active_record = TRUE;
注意: 一些CodeIgniter的類(lèi),例如Sessions,在執(zhí)行一些函數(shù)的時(shí)候需要Active Records的支持。
參數(shù)解析:
- hostname - 數(shù)據(jù)庫(kù)的主機(jī)名,通常位于本機(jī),可以表示為 "localhost".
- username - 需要連接到數(shù)據(jù)庫(kù)的用戶名.
- password - 登陸數(shù)據(jù)庫(kù)的密碼.
- database - 你需要連接的數(shù)據(jù)庫(kù)名.
- dbdriver - 數(shù)據(jù)庫(kù)類(lèi)型。如:mysql、postgres、odbc 等。必須為小寫(xiě)字母。
- dbprefix - 當(dāng)運(yùn)行Active Record查詢時(shí)數(shù)據(jù)表的前綴,它允許在一個(gè)數(shù)據(jù)庫(kù)上安裝多個(gè)CodeIgniter程序.
- pconnect - TRUE/FALSE (boolean) - 使用持續(xù)連接.
- db_debug - TRUE/FALSE (boolean) - 顯示數(shù)據(jù)庫(kù)錯(cuò)誤信息.
- cache_on - TRUE/FALSE (boolean) - 數(shù)據(jù)庫(kù)查詢緩存是否開(kāi)啟,詳情請(qǐng)見(jiàn)數(shù)據(jù)庫(kù)緩存類(lèi)。
- cachedir - 數(shù)據(jù)庫(kù)查詢緩存目錄所在的服務(wù)器絕對(duì)路徑。
- char_set - 與數(shù)據(jù)庫(kù)通信時(shí)所使用的字符集。
-
dbcollat - 與數(shù)據(jù)庫(kù)通信時(shí)所使用的字符規(guī)則。
Note: For MySQL and MySQLi databases, this setting is only used as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7 (and in table creation queries made with DB Forge). There is an incompatibility in PHP with mysql_real_escape_string() which can make your site vulnerable to SQL injection if you are using a multi-byte character set and are running versions lower than these. Sites using Latin-1 or UTF-8 database character set and collation are unaffected.
- swap_pre - A default table prefix that should be swapped with dbprefix. This is useful for distributed applications where you might run manually written queries, and need the prefix to still be customizable by the end user.
- autoinit - Whether or not to automatically connect to the database when the library loads. If set to false, the connection will take place prior to executing the first query.
- stricton - TRUE/FALSE (boolean) - 是否強(qiáng)制使用 "Strict Mode" 連接, 在開(kāi)發(fā)程序時(shí),使用 strict SQL 是一個(gè)好習(xí)慣。
-
port - 數(shù)據(jù)庫(kù)端口號(hào). 要使用這個(gè)值,你應(yīng)該添加一行代碼到數(shù)據(jù)庫(kù)配置數(shù)組。
$db['default']['port'] = 5432;
提示: 并不是所有的值都是必須的,這取決與您所使用的數(shù)據(jù)庫(kù)平臺(tái),如(MySQL, Postgres, 等.) 例如, 當(dāng)你使用SQLite時(shí),你不需要提供username 或 password, 數(shù)據(jù)庫(kù)名字就是您數(shù)據(jù)庫(kù)文件的路徑. 以上內(nèi)容假定您使用的是 MySQL 數(shù)據(jù)庫(kù).
?