【问题标题】:Detecting type of database connection in drupal检测drupal中的数据库连接类型
【发布时间】:2014-02-03 08:03:09
【问题描述】:

如何检测我在 drupal 中连接的数据库类型(使用 php 代码)?我正在尝试编写一个模块,该模块公开一些仅适用于 postgres 或 sql server 的数据库功能。目前,我通过尝试检测数据库版本来做到这一点,因为每个数据库的语法似乎都不同,但它似乎不是很健壮。有没有一个php函数会报告这个?

【问题讨论】:

标签: php sql-server postgresql drupal drupal-7


【解决方案1】:

您应该使用全局变量:$databases 并检查驱动程序值。

global $databases;

dpm($databases);

https://drupal.stackexchange.com/questions/48882/how-to-get-database-credentials

【讨论】:

    【解决方案2】:

    我目前不完全理解你在尝试什么,但是

    db_driver()
    

    以字符串形式返回他当前在 Drupal 中连接的数据库(例如“pgsql”)。

    【讨论】:

      【解决方案3】:

      这可能对您有帮助。在执行查询之前使用 db_set_active() API 设置您的数据库。

      这将帮助您避免错误

      <?php
      
      //set your configurations
      $db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
      $db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
      $db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';
      
      //set the active database and then process your queries in this case you can always knows which database is connected now.
      db_set_active('mydb');
      db_query('SELECT * FROM table_in_anotherdb');
      

      设置多个数据库

      <?php
      
      // ... header of the settings.php file
      $db_url = array (
      "default" => "mysql://user:pass@host/db",
      "second" => "pgsql://user:pass@host/db"
      );
      
      db_set_active('second');
      

      参考:https://drupal.org/node/18429

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多