CodeIgniter配置之database.php用法实例分析_PHP

WBOY
Release: 2016-05-28 11:49:07
Original
925 people have browsed it

本文实例分析了CodeIgniter配置之database.php用法。分享给大家供大家参考,具体如下:

CodeIgniter的数据库配置文件位于application/config/database.php, 该文件中定义了$db的二维数组,参考文件如下:

$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$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;
Copy after login

配置说明

代码如下:

SET SESSION sql_mode="STRICT_ALL_TABLES"


如何连接数据库?

可通过Loader中的database方式调用,即$this->load->database(); 函数的定义如下:

/**
 * Database Loader
 *
 * @param  string 数据库连接值,数组或DSN字符串传递。
 * @param  bool  是否返回数据库对象,否则将数据库对象赋值给控制器的db属性
 * @param  bool  是否使用AR,这里的设置会覆盖database.php中设置
 * @return  object
 */
function database($params = '', $return = FALSE, $active_record = NULL){}
Copy after login

$params的值有3种情况,分别是:

1、字符串,传入$db数组一维键名,如 default test等,为空则默认$active_group定义的值

2、数组,可以直接传入类似$db的一维数组,如:

$this->load->database(array(
  'hostname' => 'localhost',
  'username' => 'root',
  'password' => '123456',
  'database' => 'test',
  'dbdriver' => 'mysql',
  'pconnect' => FALSE,
  'db_debug' => TRUE,
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
));

Copy after login

3、DSN 字符串,如:

$dsn = 'mysql://root:123456@localhost/test?charset=utf8&dbcollat=utf8_general_ci';
$this->load->database($dsn);

Copy after login

PDO的初始化需要使用DSN字符串,那么在CI中该如何配置呢,可参考如下配置:

//当前版本2.x.x
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'mysql:host=localhost;dbname=test';
$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'pdo';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$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;

Copy after login

如何连接多个数据库?

$this->load->database()时会将数据库对象赋值给CI_Controller的db属性,如果已经存在了db则不会重新连接。也就是执行$this->load->database()之后再次$this->load->database('test')时则第二次load不会执行。

但load的第二个参数允许返回,所以可以返回并赋值给变量,达到连不同库的目的。

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('test', TRUE);

Copy after login

但这种方式需要使用的时候主动去load,使用不太方便,我们可以在MY_Model的构造函数中实现,将返回的$DB1重新赋值给CI_Controller的一个属性,并将该属性赋值或者clone给$this->db,例如:

public function __construct($group_name = '')
{
  parent::__construct();
  if($group_name == '') {
    $db_conn_name = 'db';
  } else {
    $db_conn_name = 'db_'.$group_name;
  }
  $CI = & get_instance();
  if(isset($CI->{$db_conn_name}) && is_object($CI->{$db_conn_name})) {
    $this->db = $CI->{$db_conn_name};
  } else {
    $CI->{$db_conn_name} = $this->db = $this->load->database($group_name, TRUE);
  }
}

Copy after login

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》和《CI(CodeIgniter)框架进阶教程》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!