External database

External database

by Pedro Remedios -
Number of replies: 4

Hi,

How would I connect to an external database using the Moodle API? Or is it a question of using the standard PHP functions?

Thanks,

Pedro Remedios

Average of ratings: -
In reply to Pedro Remedios

Re: External database

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I think you should be able to create a new instance of the appropriate moodle_database class from /lib/dml (pass true to the $external parameter of the constructor), call the connect() method with the external database's credentials, then use it like you would $DB.

In reply to Pedro Remedios

Re: External database

by Murilo Timo Neto -

Olá Pedro,

Como ví que é de portugal, vou respoder em portugues o/

Estou trabalhando em um relatório que precisa de dados de uma base de dados externa daí usei ADONewConnection

+ require_once($CFG->libdir.'/adodb/adodb.inc.php');
    + $authdb = ADONewConnection('postgres');
    + $authdb->debug = true;
    + ob_start();
    + $authdb->Connect('localhost', 'postgres', 'postgres', 'teste', true);
    + $authdb->SetFetchMode(ADODB_FETCH_ASSOC);

 

que é o mesmo método para a autenticação com banco de dados externo

https://github.com/murilotimo/moodle-block_configurablereports/commit/47c62bdc3b2d5e11516c3d6ff1c741e68b08c436

Já está quase ficando bom o/