【问题标题】:Auto loading database in codeigniter在 codeigniter 中自动加载数据库
【发布时间】:2015-09-08 18:32:13
【问题描述】:

我正在使用这个类:

<?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"];
    }
}
?>

我正在为数据库使用自动加载功能。我的配置文件:

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

但是你可以看到我没有在decrypt1 方法中使用数据库。即使我不使用数据库操作,codeigniter 会连接到数据库吗?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    这是 CI DB_Driver 类的一部分:

    /**
     * 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);
    }
    

    如您所见,直到第一次查询(DB_Driver::query() 的行为几乎相同)时,才加载数据库驱动程序(因此连接到数据库服务器)。

    另外一点,如果您在代码的前面某处执行了一些 db 查询(例如,检查用户会话/活动),那么即使您没有在您的 @ 中对 db 执行任何查询,也已经建立了与 db 服务器的连接987654324@方法。

    更新。确保在数据库配置中使用延迟加载(autoinit 选项应该是 false)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多