Codeigniter 在运行时更改数据库配置

发布于 2024-12-11 03:08:31 字数 339 浏览 0 评论 0原文

我可以更改控制器中每个方法的数据库配置吗?

$db['default']['db_debug'] = TRUE;

默认值为 TRUE,而我需要在某种方法中将其设置为 false 以捕获错误并执行其他操作(例如显示 404 页面)。

当我尝试 $this->config->load('database') 它失败了。

另一个问题:

除了将 db_debug 配置设置为 FALSE 之外,我是否可以检查不正确的查询并将其捕获到某些变量,而不是将其显示给用户?

Can I change the database config per method in a controller?

$db['default']['db_debug'] = TRUE;

The default is TRUE, while I need to make it false in a certain method to catch the error and do something else (for example show 404 page).

When I tried $this->config->load('database') it fails.

Another question :

Can I check an incorrect query and catch it to some variables rather than displaying it to users other than setting the db_debug config to FALSE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

漫漫岁月 2024-12-18 03:08:31

我检查了 system/database/DB_Driver 的代码,发现:

$this->db->db_debug = FALSE;

将在我的控制器中工作以动态启用/禁用调试功能。

I checked the code of system/database/DB_Driver and found that:

$this->db->db_debug = FALSE;

will work in my controller to enable/disable the debug thing on the fly.

白况 2024-12-18 03:08:31

扩展comenk的答案,您可以扩展数据库类并实现各种方法来实现您的目标。

首先,您需要通过创建 MY_Loader.php 文件来扩展核心 Loader 类。

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Load the Standard and/or Extended Database function & Driver class
     *
     * @access  public
     * @return  string
     */
    function database( $params = '', $return = FALSE, $active_record = NULL )
    {
        $ci =& get_instance();

        if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($ci->db) AND is_object($ci->db))
        {
            return FALSE;
        }

        $my_db      = config_item('subclass_prefix').'DB';
        $my_db_file = APPPATH.'core/'.$my_db.EXT;

        if(file_exists($my_db_file))
        {
            require_once($my_db_file);
        }
        else
        {
            require_once(BASEPATH.'database/DB'.EXT);
        }

        // Load the DB class
        $db =& DB($params, $active_record);

        $my_driver      = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
        $my_driver_file = APPPATH.'core/'.$my_driver.EXT;

        if(file_exists($my_driver_file))
        {
            require_once($my_driver_file);
            $db = new $my_driver(get_object_vars($db));
        }

        if ($return === TRUE)
        {
            return $db;
        }

        // Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        $ci->db = '';
        $ci->db = $db;
    }
}

通过实现上述内容,您将可以创建一个 MY_DB_mysqli_driver.php ,其中 mysqli 被您在 CI database.php 配置中使用的任何驱动程序替换。

此时,您将comenk的答案添加到MY_DB_mysqli_driver.php

function debug_on() {
     return $this->db_debug = TRUE;
}

function debug_off() {
     return $this->db_debug = FALSE;
}

function in_error() {
     return (bool) $this->_error_number();
}

然后在您的模型/控制器中,

$this->db->debug_off();

$this->db->query('SELECT * FROM `table`');

if( $this->db->in_error() ) {
    show_404();
}

$this->db->debug_on();

Expanding on the answer by comenk, you can extend the database class and implement various methods by which to achieve your goal.

First, you'll need to extend the core Loader class by creating a MY_Loader.php file

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Load the Standard and/or Extended Database function & Driver class
     *
     * @access  public
     * @return  string
     */
    function database( $params = '', $return = FALSE, $active_record = NULL )
    {
        $ci =& get_instance();

        if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($ci->db) AND is_object($ci->db))
        {
            return FALSE;
        }

        $my_db      = config_item('subclass_prefix').'DB';
        $my_db_file = APPPATH.'core/'.$my_db.EXT;

        if(file_exists($my_db_file))
        {
            require_once($my_db_file);
        }
        else
        {
            require_once(BASEPATH.'database/DB'.EXT);
        }

        // Load the DB class
        $db =& DB($params, $active_record);

        $my_driver      = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
        $my_driver_file = APPPATH.'core/'.$my_driver.EXT;

        if(file_exists($my_driver_file))
        {
            require_once($my_driver_file);
            $db = new $my_driver(get_object_vars($db));
        }

        if ($return === TRUE)
        {
            return $db;
        }

        // Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        $ci->db = '';
        $ci->db = $db;
    }
}

By implementing the above this will allow you to create a MY_DB_mysqli_driver.php whereby mysqli is replaced by whatever driver you're using in your CI database.php config.

At this point you'd add comenk's answer to MY_DB_mysqli_driver.php

function debug_on() {
     return $this->db_debug = TRUE;
}

function debug_off() {
     return $this->db_debug = FALSE;
}

function in_error() {
     return (bool) $this->_error_number();
}

Then in your model/controller,

$this->db->debug_off();

$this->db->query('SELECT * FROM `table`');

if( $this->db->in_error() ) {
    show_404();
}

$this->db->debug_on();
勿挽旧人 2024-12-18 03:08:31

您必须在 system/database/DB_driver.php 上添加函数,

function debug_on()
{
     $this->db_debug = TRUE;
     return TRUE;
}

function debug_off()
{
     $this->db_debug = FALSE;
     return FALSE;
}

然后您可以简单地执行此命令来在运行时进行更改

$this->db->debug_off();
$this->db->reconnect();

you must add function on system/database/DB_driver.php

function debug_on()
{
     $this->db_debug = TRUE;
     return TRUE;
}

function debug_off()
{
     $this->db_debug = FALSE;
     return FALSE;
}

after that you can simply do this command to changes at run-time

$this->db->debug_off();
$this->db->reconnect();
冷清清 2024-12-18 03:08:31

$this->db->db_debug = 0; // 0:关闭,1:打开
这对我来说...
您可以查看 $GLOBALS 变量来找到此通用设置。

$this->db->db_debug = 0; // 0: off, 1: on
That worx for me...
You can look at the $GLOBALS variable to locate this generic setting.

空城旧梦 2024-12-18 03:08:31

要向用户隐藏错误的 SQL(和其他错误),您需要设置 php 错误报告级别。 CodeIgniter 基本上以开发模式发布。

转到index.php并将其替换

error_reporting(E_ALL);

为this

error_reporting(0);

这是执行此操作的快速方法。您还可以使用钩子来实现这一点,这样您就不必接触 CI 文件。您还可以向该挂钩添加逻辑,以便它仅在生产服务器上设置它。

为了调试 SQL,您可以创建一个继承自 CI_Model 的类,然后创建所有模型类来扩展该类。在该类中,您可以添加用于运行查询的代码,将查询写入日志,以便您可以更轻松地调试它们。如果查询本身很糟糕,这将无济于事,但您应该能够在达到这一点之前弄清楚这一点。

To hide bad SQL (and other errors) from users, you need to set the php error reporting level. CodeIgniter ships in basically development mode.

Go to index.php and replace this

error_reporting(E_ALL);

with this

error_reporting(0);

This is the quick way to do it. You can also implement this using a hook, so you don't have to touch CI files. You can also add logic to that hook so that it only sets it on the production server.

For debugging SQL, you can create a class that inherits from CI_Model, then create all your model classes to extend that class. In that class, you can add code for running queries that writes the queries to the log so that you can debug them easier. This won't help if the query itself is bad, but you should be able to figure that out before you get to that point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文