Home >Database >Mysql Tutorial >CI 框架自定义数据库连接配置

CI 框架自定义数据库连接配置

WBOY
WBOYOriginal
2016-06-07 15:35:521519browse

?phpclass My_model extends CI_Model{public function __construct(){parent::__construct();$db_conn['hostname'] = localhost;$db_conn['username'] = root;$db_conn['password'] = nagiosxi;$db_conn['database'] = web;$db_conn['dbdriver'] = mysql;$d

<?php class My_model extends CI_Model
	{
		public function __construct()
		{
			parent::__construct();
			$db_conn['hostname'] = "localhost";
			$db_conn['username'] = "root";
			$db_conn['password'] = "nagiosxi";
			$db_conn['database'] = "web";
			$db_conn['dbdriver'] = "mysql";
			$db_conn['dbprefix'] = "";
			$db_conn['pconnect'] = FALSE;
			$db_conn['db_debug'] = TRUE;
			$db_conn['port'] = 3306;
			$this->load->database($db_conn); 
			//从此处提交配置参数,可以自定义数据库连接,可以有多个数据库的连接,不依靠config项里的单一数据库配置。
			//另一处自定义配置数据库连接参数的地方是在调用模块的地方
			//$this->load->mode('My_model', '别名',$db_conn); //别名可以无
		}
	}
?>

也可以在./config/database.php 里定义数据库连接数组,在调用$this->load->database('组名');

例:

./config/database.php

   $config['my']['hostname'] = 'localhost';

   $config['my']['username'] = 'root';

调用:

 $this->load->database('my');

http://codeigniter.org.cn/user_guide/database/connecting.html 这是说明地址

Statement:
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