doudun2212 2015-09-08 18:32
浏览 75
已采纳

在codeigniter中自动加载数据库

I am using this class:

<?php
class Decrypt extends CI_Controller
{



    public function decrypt1($toDescrypt="")
    {
        $this->load->library('encrypt');
        $toDescrypt = urldecode($toDescrypt);
        $s=unserialize($this->encrypt->decode($toDescrypt));
        echo $s["username"];
    }
}
?>

And I am using auto load feature for database. My config file:

$autoload['libraries'] = array('database','session');

But you can see I am not using database in decrypt1 method. Will codeigniter connect to database even i don't use the database operation?

  • 写回答

1条回答 默认 最新

  • dongwu8653 2015-09-08 18:54
    关注

    This is a portion of CI DB_Driver class:

    /**
     * Simple Query
     * This is a simplified version of the query() function.  Internally
     * we only use it when running transaction commands since they do
     * not require all the features of the main query() function.
     *
     * @access  public
     * @param   string  the sql query
     * @return  mixed
     */
    function simple_query($sql)
    {
        if ( ! $this->conn_id)
        {
            $this->initialize();
        }
    
        return $this->_execute($sql);
    }
    

    As you can see, database driver isn't loaded (and thus connected to db server) until the first query (DB_Driver::query() behaves almost same).

    Another point, if somewhere earlier in the code you performed some db queries (like, checking user session/activity), then connection to db server is already established, even if you don't perform any queries to db in your's decrypt1() method.

    Upd. Make sure to use lazy-loading in database config (autoinit option should be false).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化