joomla中的mysql_select_db

时间:2015-04-14 10:06:37

标签: php mysql joomla

使用2个数据库,我想在joomla查询之前选择数据库,类似于核心php中的mysql_select_db

目前我使用下面的行来触发所有查询,

$db =& JFactory::getDBO();

我不想这样查询,

select * from databasename.tablename; 

因为它影响了我的mysql复制。

有人遇到过类似的问题吗?

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

创建一个像这样的辅助函数

function getCustomDBO(){   
    jimport('joomla.database.database');
    jimport( 'joomla.database.table' );

    $conf =& JFactory::getConfig();

    $host       = '192.168.1.1'; //replace your IP or hostname
    $user       = 'db_user'; //database user
    $password   = 'db_pass';//database password
    $database   = 'db_name'; //database name
    $prefix     = 'jos_'; //prefix if any else just give any random value
    $driver     = 'mysql'; //here u can also have ms sql database driver, postgres, etc
    $debug      = $conf->getValue('config.debug');

    $options    = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );

    $db =& JDatabase::getInstance( $options );

    if ( JError::isError($db) ) {
        header('HTTP/1.1 500 Internal Server Error');
        jexit('Database Error: ' . $db->toString() );
    }

    if ($db->getErrorNum() > 0) {
        JError::raiseError(500 , 'JDatabase::getInstance: Could not connect to database <br />' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );
    }

    $db->debug( $debug );
   }

然后你就可以像SomeHelper :: getCustomDBO()那样调用它;

享受!!