Codeigniter change database config at runtime

前端 未结 5 1635
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 00:44

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

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

The default is TRUE, while

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 01:12

    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.

提交回复
热议问题