I’ve been recently asked how you can connect to a database server different than the “master”, that is the server where the PHPDS installation resides.

Actually it’s very easy as long as the server is Mysql:

class test_connector extends PHPDS_legacyConnector
{
 protected function applyConfig($db_config = '')
 {
  $this->dbHost = "localhost";
  $this->dbName = "test";
  $this->dbUsername = "username";
  $this->dbPassword = "userpassword";
 }
}
class test_query extends PHPDS_query
{
 protected $connector = "test_connector";
 protected $sql = "SHOW TABLES";
}
  $tables = $this->db->invokeQuery('test_query');

As you see, a query will use a different database connector if it’s provided in its field. Just give the class name of a connector class and the framework will handle the rest.

In this case, we used the “legacy” connector, which is the Mysql connector used all around by the framework. If you want to connect to another database software, you’ll have to write a custom connector (it’s not a big deal actually, just remapping the library function to class methods).

In a future version, you’ll be able to set the actual parameters in the configuration file, like the “master” database.

See How can I interact with a remote database in my own plugins?

Feedback

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Post Comment