CodeIgniter Forums
Best way to use db table prefix - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Best way to use db table prefix (/showthread.php?tid=74377)



Best way to use db table prefix - bustersg - 09-17-2019

So I include $db = ... in every functions
and in each RAW $sql, I use $db->prefixTable('table_name')

It works but is this the ideal way of coding it in CI4?

PHP Code:
function get_include_files()
{
    $db = \Config\Database::connect();

    $include_files = array(); // must return something

    $sql "SELECT plugin_id, plugin_includes FROM " $db->prefixTable('plugins') . " WHERE plugin_enabled = 1";
    ... 

Thanks.


RE: Best way to use db table prefix - MGatner - 09-18-2019

If you define a prefix on your connection configuration then using the query builder will automatically prefix all you table names.