codeigniter $ this-> db-> reconnect();用法 [英] Codeigniter $this->db->reconnect(); usage

查看:570
本文介绍了codeigniter $ this-> db-> reconnect();用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动加载数据库,因为我的应用程序的大多数页面需要数据库处理,否则整个事情会减慢。我想做的是不是建立一个新的连接到db,当有一个已经活着,并使用它,而不是打扰server-db。那么如何实现 $ this-> db-> reconnect(); 到我的例子下面?我读了用户指南,但没有确切的例子。

I’m not autoloading db because most pages of my application don’t need db processing otherwise whole thing will slow down. What I want to do is not to establish a new connection to db when there is one already alive and use it instead not to bother server-db. So how do I implement $this->db->reconnect(); to my example below? I read user guide but no exact example there.

注意:如果我需要使用 $ this-& db-> close(); $ this-> db-> initialize(); 因为我听说在禁用自动加载的情况下调用 $ this-> db-> reconnect(); 会引发错误。

Note : If I necessary to use $this->db->close(); and $this->db->initialize(); then please help me implement them as well because I heard that calling $this->db->reconnect(); with autoloading disabled will throw an error.

我使用CI 2.1

感谢

class Test_model extends CI_Model
{
 public function __construct()
 {
  parent::__construct();

  $this->load->database();
 }

 public function function_a($id)
 {
  $this->db->protect_identifiers('year');

  $sql = "SELECT * FROM year WHERE id = ?";

  $data['dbquery'] = $this->db->query($sql, array($id));

  return $data['dbquery'];
 }

 public function function_b($id)
 {
  $this->db->protect_identifiers('month');

  $sql = "SELECT * FROM month WHERE id = ?";

  $data['dbquery'] = $this->db->query($sql, array($id));

  return $data['dbquery'];
 }


 public function function_c($id)...
 public function function_d($id)...
 public function function_e($id)...
} 


推荐答案

如果您在 database.php 中保留 pconnect = false ,那么连接将在每个脚本执行结束时自动关闭并且默认情况下它设置为 false

Basically if you keep pconnect=false in database.php then the connection will be closed at the end of each script’s execution automatically and by default it set to false.

好吧,如果你想要的话,你可以使用

Well if you want then you can use

$this->db->close();

每次执行查询后手动关闭连接并使用

after every query execution to close the connection manually and use

$this->db->initialize();

在任何查询执行之前,在关闭连接后再次初始化连接。

before any query execution to initialize the connection again after closing it.

这篇关于codeigniter $ this-> db-> reconnect();用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆