• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Connecting to remote MySQL database

#1
Example code:
PHP:
<?php
class Test_Callback {
 
    public static function testQuery(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response) {
        $mydb = XenForo_Application::get('db');

        $testQuery = $mydb->fetchAll("
        SELECT username,avatar_date
        FROM `xf_user`
        WHERE `avatar_date` > 0
        ORDER BY `avatar_date` DESC");

        $response->params['testQuery'] = $testQuery;
    }
}
Simple question: How would I change it to connect to a remote MySQL database instead of the XF database?
 
Last edited:

Mike

XenForo developer
Staff member
#2
You need to create a new database connection. Adjust this code to suit:
Code:
$db = Zend_Db::factory('mysqli',
    array(
        'host' => $config['db']['host'],
        'port' => $config['db']['port'],
        'username' => $config['db']['username'],
        'password' => $config['db']['password'],
        'dbname' => $config['db']['dbname'],
        'charset' => $config['db']['charset']
    )
);