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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器