Reputation: 19154
How can I detect what type of database I'm connecting to in drupal (using php code)? I am trying to write a module which exposes some database functionality which only works on postgres or sql server. Currently, I'm doing it by trying to detect the database version, since the syntax appears to be different for each database but it doesn't seem very robust. Is there a php function which will report this?
Upvotes: 0
Views: 1058
Reputation: 41
I don't fully understand what you're trying at the moment but
db_driver()
returns he currently connected database in Drupal as a string (e.g. "pgsql").
Upvotes: 1
Reputation: 161
You should use the global variable: $databases
and check the driver value.
global $databases;
dpm($databases);
https://drupal.stackexchange.com/questions/48882/how-to-get-database-credentials
Upvotes: 2
Reputation: 4650
This may be helpfull to you. Use db_set_active() API to set your database before executing the query.
This will help you to keep away from errors
<?php
//set your configurations
$db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';
//set the active database and then process your queries in this case you can always knows which database is connected now.
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
Setting multiple databases
<?php
// ... header of the settings.php file
$db_url = array (
"default" => "mysql://user:pass@host/db",
"second" => "pgsql://user:pass@host/db"
);
db_set_active('second');
Ref: https://drupal.org/node/18429
Upvotes: 0