0

Is there a straightforward solution to do the dynamic select statements against another database on the same server? I fooled around with attempting to modify the documentation example to no avail.

// Create an object of type SelectQuery and directly 
// add extra detail to this query object: a condition, fields and a range
$query = db_select('custom_module_db', 'table_foo')
  ->condition('table_foo.id', 0, '>')
  ->fields(array('id', 'field_1', 'field_2'));

$result = $query->execute();

My current database setup looks like this

AWS RDS Server 
 ->production drupal database
   ->Drupal Installation Tables
 ->custom module database
   ->Custom Module Tables

Clarification the below method works fine against the custom module database without adding a specific settings reference for that particular database.

So was looking to see if I can use the db_select() function in the same way as db_query() without needing to go in and add another database reference.

 $data_set = db_query($query='SELECT * FROM custom_module.db.table_foo WHERE ......)

Then grabbing the resultset by

$results = $this->WeightedAverageFunction($data_set->fetchAll())
1

Add external database name in settings.php(../sites/all/default/settings.php) file of your project. You can set databse in settings.php as follows:

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'drupal_db',
      'username' => 'root',
      'password' => '',
      'host' => 'localhost',
      'port' => '8080',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),

  'external' => 
  array (
    'default' => 
    array (
      'database' => 'external_db',
      'username' => 'root',
      'password' => '',
      'host' => 'localhost',
      'port' => '8080',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

And you can directly add your query in files.

  • not that its a major inconvenience or a roadblock in my project, but is there a way to do it without adding in the additional database reference? I detailed above how I do it right now without adding that reference. But, what I was really looking for was how to use the db_select() without needing to add another reference in the settings.php file. – gcalex5 May 12 '16 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.