Attempting to create an external script that allows me to query some things in Drupal. I was hoping to be able to use my external databases I have defined in the settings of my site to query some of their tables. As one of the databses is Oracle based I have the http://drupal.org/project/oracle module installed.

The following is a PHP script outside of PHP but on the same server (different host):

$_SERVER['HTTP_HOST'] = 'www.example.com';

define('DRUPAL_ROOT','/usr/share/drupal7/');

//require the bootstrap include
require_once DRUPAL_ROOT.'./includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 
$db1 = Database::getConnection('oracle','external');
$sql = "SELECT * from accounts";
$result = $db1->query($sql);
$accounts = $result->fetchObject();
print_r($accounts);

This code errors with:

PDOException: could not find driver in main()

The same code (just the Database::getConnection down) running from within a Drupal page works fine. I can execute other Drupal query code just fine from a mysql database.

Possibly related to http://drupal.org/node/1504352 - may need to change driver name to oci to match PDO?

Comments

radalin’s picture

Hi, your issue is definitely not related to the issue you have referenced. I have checked the code you have written on my own server, and it works.

If you are running this code on the CLI perhaps your php.ini for apache and cli are not the same and pdo_oci may not be included in the cli/php.ini.

Apart from that are you sure that the parameters you have passed to getConnection() are right? I have looked to the code for the Database object, and getConnection() does not take the driver (I assumed that because you have passed the oracle as the first parameter) but the target db's name. Make sure that your sites/default/settings.php (or similar) is something like this:

$databases = array (
  'default' =>//this is the first parameter
  array (
    'default' => //this is the second parameter
    array (
      'database' => 'oracledb',
      'username' => 'user',
      'password' => 'secret',
      'host' => 'oracle',
      'port' => '',
      'driver' => 'oracle',
      'prefix' => '',
    ),
  ),
);

The code I have tried:

$_SERVER['HTTP_HOST'] = 'www.example.com';

define('DRUPAL_ROOT','/home/roy/public_html/drupal2');

//require the bootstrap include
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$db1 = Database::getConnection('default','default'); //default and default as defined in the settings.php file.
$sql = "SELECT * from variable";
$result = $db1->query($sql);
$accounts = $result->fetchAll();
print_r($accounts);
bohart’s picture

Version: 7.x-1.12 » 7.x-1.x-dev
Issue summary: View changes
Status: Active » Closed (outdated)

D7 reached its EOL back in January 2025, and there is no active release for D7 for this module anymore.
Development or support is not planned for D7. All D7-related issues are marked as outdated in a bunch.

Everyone can apply the patches/suggestions above (not tested by the maintainers, tested by the community) to their D7 projects.
If the issue remains relevant for D10+ versions, merge requests with proposed solutions for a new module version (D10+) are welcome in a new follow-up issue.

Thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.