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的数据库的用户有权限的数据库要连接到? –