0
基本上我只想連接到相同的主機,但在Joomla模塊中的不同數據庫。所以我怎麼能從配置中獲得用戶/傳遞信息,以避免需要硬編碼或參數化信息,因爲它已經存在。如何使用默認的Joomla數據庫用戶名和密碼連接到另一個數據庫?
感謝
基本上我只想連接到相同的主機,但在Joomla模塊中的不同數據庫。所以我怎麼能從配置中獲得用戶/傳遞信息,以避免需要硬編碼或參數化信息,因爲它已經存在。如何使用默認的Joomla數據庫用戶名和密碼連接到另一個數據庫?
感謝
您可以使用此代碼(這是相同的代碼,這是在JFactory :: getDBO()方法)。
jimport('joomla.database.database');
jimport('joomla.database.table');
$conf = JFactory::getConfig();
$host = $conf->get('host');
$user = $conf->get('user');
$password = $conf->get('password');
$database = 'YOUR_DATABASE_NAME';
$prefix = $conf->get('dbprefix'); //***Change this if the dbprefix is not the same!***
$driver = $conf->get('dbtype');
$options = array ('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);
$db = JDatabase::getInstance($options);
我希望它有幫助!
獲取配置值如下:
$app = JFactory::getApplication();
echo $app->getCfg('user');
echo $app->getCfg('password');
以防萬一,因爲你沒有提到它,我會問 - 沒有Joomla的數據庫的用戶有權限的數據庫要連接到? –