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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵