php Codeigniter:多次使用 $this->load->database() 会导致多个连接吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17420036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter: Will using $this->load->database() multiple times result in multiple connections?
提问by Lukas Oppermann
I was thinking about removing the database library from my autoload file. I am caching everything on the page so I do not need to connect to the db every time and I figured it is a waste of speed if I have CI connect to the db anyway.
我正在考虑从我的自动加载文件中删除数据库库。我正在缓存页面上的所有内容,所以我不需要每次都连接到数据库,我认为如果我让 CI 连接到数据库,这会浪费速度。
My Question is now: if I for e.g. load the database in MY_Model $this->load->database()
without parameters (they come from the config file), will a new connection be established for every model I use in a controller?
我现在的问题是:如果我在 MY_Model 中加载数据库$this->load->database()
而没有参数(它们来自配置文件),是否会为我在控制器中使用的每个模型建立一个新连接?
Or will it be like helpers that are only loaded once?
或者它会像只加载一次的助手一样?
回答by Expedito
They only get loaded once. The database class makes sure of that for you.
它们只加载一次。数据库类为您确保这一点。
回答by Kees Sonnema
No, You only will connect to one database. The one you specified in database.php
不,您只会连接到一个数据库。你指定的那个database.php
You could however connect to multiple databases.
Say you have two connections in database.php
like the following:
但是,您可以连接到多个数据库。假设您有两个连接,database.php
如下所示:
/* FORUM */
$active_group = "forum";
$active_record = TRUE;
$db['forum']['hostname'] = "xxxxx";
$db['forum']['username'] = "xxxxx";
$db['forum']['password'] = "xxxxx";
$db['forum']['database'] = "xxxxx";
$db['forum']['dbdriver'] = "mysql";
$db['forum']['dbprefix'] = "";
$db['forum']['pconnect'] = TRUE;
$db['forum']['db_debug'] = TRUE;
$db['forum']['cache_on'] = FALSE;
$db['forum']['cachedir'] = "";
$db['forum']['char_set'] = "utf8";
$db['forum']['dbcollat'] = "utf8_general_ci";
/* TEST SITE */
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "xxxxx";
$db['default']['username'] = "xxxxx";
$db['default']['password'] = "xxxxx";
$db['default']['database'] = "xxxxx";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
You can call this databases using:
您可以使用以下方法调用此数据库:
$this->db_forum = $this->CI->load->database('forum', TRUE);
Acces db1 like $this->db
访问 db1 喜欢 $this->db
And db2 like $this->db_forum
和 db2 一样 $this->db_forum
You can look at the CI docs for more information: http://ellislab.com/codeigniter/user-guide/database/connecting.html
您可以查看 CI 文档以获取更多信息:http: //ellislab.com/codeigniter/user-guide/database/connecting.html
I just used a simple example.
我只是用了一个简单的例子。
Hope it helps.
希望能帮助到你。
回答by Robin Castlin
Upon loading a model, you can use this:
加载模型后,您可以使用:
$this->load->model('my_model', NULL, TRUE);
The third argument will connect to the database by itself.
第三个参数将自行连接到数据库。
Found this by looking into the system/core/Loader.php
.
通过查看system/core/Loader.php
.
And in the same file, $this->load->database();
is run and returns FALSE
before connection if $this->db
is already set, so no worries about multiple connects:
并且在同一个文件中,如果已经设置,则在连接之前$this->load->database();
运行并返回,所以不用担心多个连接:FALSE
$this->db
// Do we even need to load the database class?
if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
{
return FALSE;
}
回答by Tomas Lopez
I got it working very easy, I have a site which choses a database based on the login username, so I have a "main" database that contains the information of the dynamic selectable database. the way I did is place this code where I need to change the database as follows:
我让它工作起来很容易,我有一个根据登录用户名选择数据库的站点,所以我有一个包含动态可选数据库信息的“主”数据库。我的做法是将这段代码放在我需要更改数据库的地方,如下所示:
$db1['hostname'] = 'localhost';
$db1['username'] = $fila['dbusername'];
$db1['password'] = $fila['dbpass'];
$db1['database'] = $fila['dbname'];
$db1['dbdriver'] = 'mysqli';
$db1['dbprefix'] = 'ospos_';
$db1['pconnect'] = FALSE;
$db1['db_debug'] = TRUE;
$db1['cache_on'] = FALSE;
$db1['cachedir'] = '';
$db1['char_set'] = 'utf8';
$db1['dbcollat'] = 'utf8_general_ci';
$db1['swap_pre'] = '';
$db1['autoinit'] = TRUE;
$db1['stricton'] = FALSE;
$this->db->close();
$this->load->database($db1,FALSE);
If you want to make it permanent once the DB is selected I suggest to use a session var and modify the autoload.php file something like:
如果您想在选择数据库后使其永久化,我建议使用会话变量并修改 autoload.php 文件,例如:
class DatabaseLoader {
public function __construct() {
$this->load();
}
public function load() {
$CI =& get_instance();
$may_db=$CI->session->userdata('may_db');
if($may_db)
{
$CI->db = $CI->load->database($may_db, TRUE);
}
else
{
$CI->db = $CI->load->database('default', TRUE);
}
}
}
And add the the db info to a session var in the first section of code:
并将数据库信息添加到代码的第一部分中的会话变量中:
$this->session->set_userdata('may_db',$db1);