giovedì 6 giugno 2013

Joomla: how to connect to an EXTERNAL DB

Installing and Configuring
  • Install the plugin called externaldb, then make sure you enable it!
  • In Plugin Manager, configure your external database details for the database(s) you need. 
    • DSN: Your name for the database to be used later on
    • Hostname: Host where the MySQL server is
    • Port Number: Defaults to 3306, but can change if needed
    • Username / Password : Authentication details for the database
    • Database Name: Name of the MySQL database to use
Implementing

The following code is a solution to using external databases. There is no way of using this without modifying some code. That out of the way, here's how to use it in that code.

You can use Jumi module to insert PHP code in your article in joomla page.
ExternalDB simply creates a new jDatabase object with your given connection details - nothing clever but simple to maintain connection details. Here's a sample of code from one of our components...

// Create new ExternalDB instance, using the details that we set earlier.  
// 'resultsDb' is what we called the DSN
$dbxo = new externaldatabase ( 'resultsDb' );  
// Create a new jDatabase object, connected with the details from the above selected DSN.  
// We call ExternalDB objects $dbx just as a standard practice
$dbx = $dbxo->getDB ();
$sql = "SELECT * FROM sport WHERE site = 2"; // Some simple SQL..
// Now you use your ExternalDB object in exactly the same way as you would the regular DB object..
$dbx->setQuery ( $sql );
$dbx->query ();
$res = $dbx->loadAssocList ();

Nessun commento: