数据库配置
CodeIgniter 有一个配置文件让你存放数据库连接值(username:用户名,password:密码,database name:数据库名,等等..). 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.
配件文件存放在一个如下格式的一个多维数组里:
$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;
我们使用多维数组的原因是为了让你随意的存储多个连接值的设置。举例:如果你运行多个环境(development:开发、production:制作、test:测试 等等..),你能为每个环境建立独立的连接组,并在组直接进行切换。举例,设置一个"test"环境,你可以这样做:
$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;
那么,告诉系统使用"test"组,你可以设置位于配置文件中的变量:
$active_group = "test";
注意: "test"的名字是任意的,这可以让你自由设置,我们的主要连接默认使用"default"这个名字,当然,您可以基于您的项目为它起一个更有意义的名字。
Active Record
Active Record 类 可以通过数据库配置文件里的$active_record变量进行全局的设定(允许/禁止 TRUE/FALSE (boolean)). 如果你不用这个类,那么你可以通过将这个变量值设置成FALSE来减少在数据库类初始化时对电脑资源的消耗。
$active_record = TRUE;
注意: 一些CodeIgniter的类,例如Sessions,在执行一些函数的时候需要Active Records的支持。
参数解析:
- hostname - 数据库的主机名,通常位于本机,可以表示为 "localhost".
- username - 需要连接到数据库的用户名.
- password - 登陆数据库的密码.
- database - 你需要连接的数据库名.
- dbdriver - 数据库类型。如:mysql、postgres、odbc 等。必须为小写字母。
- dbprefix - 当运行Active Record查询时数据表的前缀,它允许在一个数据库上安装多个CodeIgniter程序.
- pconnect - TRUE/FALSE (boolean) - 使用持续连接.
- db_debug - TRUE/FALSE (boolean) - 显示数据库错误信息.
- cache_on - TRUE/FALSE (boolean) - 数据库查询缓存是否开启,详情请见数据库缓存类。
- cachedir - 数据库查询缓存目录所在的服务器绝对路径。
- char_set - 与数据库通信时所使用的字符集。
-
dbcollat - 与数据库通信时所使用的字符规则。
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) - 是否强制使用 "Strict Mode" 连接, 在开发程序时,使用 strict SQL 是一个好习惯。
-
port - 数据库端口号. 要使用这个值,你应该添加一行代码到数据库配置数组。
$db['default']['port'] = 5432;
提示: 并不是所有的值都是必须的,这取决与您所使用的数据库平台,如(MySQL, Postgres, 等.) 例如, 当你使用SQLite时,你不需要提供username 或 password, 数据库名字就是您数据库文件的路径. 以上内容假定您使用的是 MySQL 数据库.