13

I sometimes access the Joomla DB via external scripts. The problem I have is that if I ever need to change the prefix, I'll need to edit my scripts again too.

Is there a way to programmatically determine the Joomla database prefix using php? If yes, how?

2 Answers 2

16

You can get the prefix using the following:

For Joomla 2.5 use:

$app = JFactory::getApplication(); 
$prefix = $app->getCfg('dbprefix');

For Joomla 3.2 use:

$app = JFactory::getApplication(); 
$prefix = $app->get('dbprefix');

But Joomla documentation recommends that you never modify the prefix. I know some backup software allows you to make a change part way through a restore though don't underestimate the work potentially involved.

Source: http://docs.joomla.org/How_to_determine_your_database_prefix

1
  • 4
    Joomla Documentation doesn't mean that you must never modify the db_prefix. It just says to not change the value, in the global configuration settings, because this will obviously break the site - by loosing the connection to the database. Changing the value in com_config is not considered a db prefix change, as it doesn't make any changes to the database itself. Database prefix can be changed, assuming someone knows what he is doing.
    – FFrewin
    Apr 26, 2014 at 16:12
4

If you are accessing the Joomla DB via external scripts, maybe you don't want to initialize all joomla stuffs only to get the database prefix. So, you can require the configuration.php file inside your script, instantiate the JConfig class and get the dbprefix

require_once $path_to_joomla_folder . "/configuration.php";
$config = new JConfig;
$dbprefix = $config->dbprefix;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.