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

时间:2017-02-02 11:03:11

标签: 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>
    
  3. 现在,当我加载数据(刷新页面)时,我收到错误,如

        '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的新手。在这方面,任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

虽然根据文档,你设置第二个连接的方式没有任何问题,我从来没有能够像这样工作。我使用它的唯一方法是使用下面的方法,虽然我同意它似乎没有任何根本的不同:​​

$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(&#39; second_database&#39;)。

未找到&#34;基表或视图:1146表&#39; test.semaphore&#39;不存在&#39;&#34;消息通常是当Drupal尝试在仍然连接到第二个数据库的同时进行Drupally时,所以不要忘记执行db_set_active()以将连接返回到Drupal数据库。无需添加&#39;默认&#39;默认为假定的默认数据库。

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

之后

或者,如果您只需要在一个地方/偶尔使用此连接,您可以动态设置它。更多信息here

希望这有帮助