无法从 Drupal 7 中的其他数据库获取数据

Not able to fetch the data from other database in Drupal 7

我正在尝试从 Drupal 7 中的另一个数据库获取数据。但是我无法获取数据。这是我试过的代码。

  1. 我在settings.php

    中添加了另一个数据库信息
    $databases['default']['default'] = array(
      'driver' => 'mysql',
      'database' => 'drupal_testing',
      'username' => 'root',
      'password' => '',
      'host' => 'localhost',
      'prefix' => '',
    );
    $databases['sakshi']['default'] = array(
      'driver' => 'mysql',
      'database' => 'test',
      'username' => 'root',
      'password' => '',
      'host' => 'localhost',
      'prefix' => '',
    );
    
  2. 并在 page.tpl.php 中添加了用于测试连接的代码。

    <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 padtop30">
                <h4> DB Connection test </h4>
                <?php 
                try{
                    echo "inside try block";
                    db_set_active('sakshi');
                    $results = db_query("select name from test.user_names where phone = 432323");
                    $records = $results->fetchAll();
                    foreach ($records as $record) {
                      echo $record;
                    }
                }catch(\PDOException $ex){
                    echo "inside catch block";
                    echo $ex;
                }finally{
                    echo "finally block is executed";
                    db_set_active('default');
                }   
                ?>
            </div>
    

现在,当我加载数据(刷新页面)时,出现类似

的错误
    'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.semaphore' doesn't exist' in E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc:2227 Stack trace: #0 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2227): PDOStatement->execute(Array) #1 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(697): DatabaseStatementBase->execute(Array, Array) #2 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2406): DatabaseConnection->query('SELECT expire, ...', Array, Array) #3 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(167): db_query('SELECT expire, ...', Array) #4 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(146): lock_may_be_available('rules_get_cache...') #5 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(368): lock_acquire('rules_get_cache...', 60) #6 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(1026): rules_get_cache('event_watchdog') #7 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\modules\events.inc(180): rules_invoke_event('watchdog', Array) #8 [internal function]: rules_watchdog(Array) #9 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\module.inc(926): call_user_func_array('rules_watchdog', Array) #10 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(1997): module_invoke('rules', 'watchdog', Array) #11 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(210): watchdog('php', '%type: !message...', Array, 3) #12 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(75): _drupal_log_error(Array, true) #13 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(2576): _drupal_error_handler_real(4096, 'Object of class...', 'E:\xampp5.6.20\...', 321, Array) #14 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\themes\my_theme\page.tpl.php(321): _drupal_error_handler(4096, 'Object of class...', 'E:\xampp5.6.20\...', 321, Array) #15 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1526): include('E:\xampp5.6.20\...') #16 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1208): theme_render_template('sites/all/theme...', Array) #17 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(6045): theme('page', Array) #18 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(5907): drupal_render(Array) #19 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(2748): drupal_render_page('

我是 Drupal 新手。在这方面谁能帮帮我。

虽然根据文档,您设置第二个连接的方式没有任何问题,但我一直无法让它像那样工作。我让它工作的唯一方法是使用下面的方法,尽管我同意这似乎没有什么根本不同:

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

  'second_database' =>
    array (
      'default' =>
        array (
           'driver' => 'mysql',
           'database' => 'second_db',
           'username' => 'root',
           'password' => 'password',
           'host' => 'localhost',
           'port' => '',
           'prefix' => '',
        ),
   ),
);

那么你应该可以做到 db_set_active('second_database').

"Base table or view not found: 1146 Table 'test.semaphore' doesn't exist'" 消息通常是在 Drupal 尝试在 Drupally 上执行某些操作时仍然附加到您的第二个数据库时,所以不要忘记执行 db_set_active() 到 return连接回 Drupal 数据库。无需添加 'default',因为默认是假定的默认数据库。

您应该在获取所需数据后直接执行此操作。所以在你的例子中,它应该在 $records = $results->fetchAll();

之后

或者,如果您只在一个地方/不经常需要此连接,您可以即时设置它。更多信息 here

希望对您有所帮助