Access another database from MODx

From MODx Wiki

Jump to: navigation, search

So you want to access an external database from MODx snippet / plugin / etc. and don't know how? Here are a couple solutions.

Different Database, Same Server

If the database is on the same MySQL server as your MODx database, you can access the database simply by typing the name of the database followed by a period.

$ds = $modx->db->select('*','database_name.table_name');
  • NOTE: Your MODx MySQL user must also have necessary privileges for the alternate database.
  • See API:DBAPI:select for more information.

Different Database, Different Server

If the database is located on another server or you want to access it using different user, you need to create a new DBAPI connection to your database:

$db = new DBAPI($host,$database, $uid,$pwd);
$ds = $db->select('*','table_name');

Resources

Personal tools