2

When I connect my custom module with database external

$database = array(
  'host' => 'localhost', 
  'database' => 'DATABASE1',
  'username' => 'root', 
  'password' => 'root', 
  'driver' => 'mysql',
);
Database::addConnectionInfo('mykey', 'default', $database);
   db_set_active('mykey');

it gives error on running the module

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'DATABASE1.filter_format' doesn't exist: SELECT ff.* FROM {filter_format} ff WHERE (status = :db_condition_placeholder_0) ORDER BY weight ASC; Array ( [:db_condition_placeholder_0] => 1 ) in filter_formats() (line 434 of /var/www/html/drupalprac/modules/filter/filter.module).

Uncaught exception thrown in shutdown function.

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'DATABASE1.semaphore' doesn't exist: DELETE FROM {semaphore} WHERE (value = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => 1595456215ac3545f2ff1c3.60338979 ) in lock_release_all() (line 269 of /var/www/html/drupalprac/includes/lock.inc). Uncaught exception thrown in session handler.

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'DATABASE1.sessions' doesn't exist: SELECT 1 AS expression FROM {sessions} sessions WHERE ( (sid = :db_condition_placeholder_0) AND (ssid = :db_condition_placeholder_1) ); Array ( [:db_condition_placeholder_0] => gSAxKMpfVXzf2EVP4EWBtuF2klcg0RZie30ztTTUjKc [:db_condition_placeholder_1] => ) in _drupal_session_write() (line 209 of /var/www/html/drupalprac/includes/session.inc).

1 Answer 1

2

You need to switch the database back before it carries out any operation that requires the Drupal database.

// Switch to your custom database.
db_set_active('mykey');

// Work with that database (select/insert/update).
...

// Switch back, before you use the Drupal API again
db_set_active();    
4
  • 1
    I have already done this in my code after the return statement but getting this error Commented Apr 3, 2018 at 11:20
  • 2
    There's no return statement in your code. But regardless, logically speaking, you must be doing something that requires the Drupal database connection before you switch it back again. Refactor your code so that the only thing you're doing in between switching away from then back to the Drupal DB connection is database operations. Nothing using the Drupal API
    – Clive
    Commented Apr 3, 2018 at 11:24
  • I created a function in my custom module where firstly I set active external database then sending a query to retrieve data and print it in tabular theme form the after return statement of function, switch back to the default database. So where m i wrong? Please help Commented Apr 4, 2018 at 5:39
  • See the answer, that's as much as I can possibly tell you from the information you've provided
    – Clive
    Commented Apr 4, 2018 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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