【问题标题】:How to get the list of table names from database in Drupal如何从 Drupal 中的数据库中获取表名列表
【发布时间】:2010-10-27 06:16:24
【问题描述】:

我想将数据库中的表列表放入数组中,并将特定表的列名放入 Drupal 中的数组中。请提及 Drupal 中的查询。谢谢

【问题讨论】:

    标签: mysql database drupal


    【解决方案1】:

    如果您想生成的结构不是数据库中的实际内容,而是您激活的模块如何定义它,您可以为激活的模块调用hook_schema。实际上有一个 API 调用它,所以你所要做的就是调用drupal_get_schema

    这是获取信息的简单方式,但它不会触及数据库,因此任何使用 SQL 手动创建的表,或不是来自 Drupal 的表,或用原始 SQL 制作的将不会被找到。但是,在 99.9% 的情况下,它是准确的。

    SQL:

    SHOW TABLES;
    SHOW COLUMNS FROM table_name;
    

    【讨论】:

      【解决方案2】:

      试试这个

      global $db_url;
      $db_name = explode("/",$db_url);
      $dbname = $db_name[count($db_name)-1];
      $tables_list = db_query("SHOW tables FROM ".$dbname." WHERE Tables_in_".$dbname." LIKE 'acc%'");
      $list_of_tables = array();
      while ($result = db_fetch_array($tables_list)) {
      drupal_set_message(t('Table name : @db',array('@db'=>$result['Tables_in_'.$dbname.''])));
      $list_of_tables[] = $result['Tables_in_'.$dbname.''];
      }
      //$list_of_tables array contains tables in database.
      
      $columns = db_query("SHOW FIELDS FROM node");
      $list_of_columns = array();
      while ($res = db_fetch_array($columns)) {
      drupal_set_message(t('Column name : @c',array('@c'=>$res['Field'])));
      $list_of_columns[] = $res['Field']; 
       }
      //$list_of_columns contains columns in the node table.
      

      【讨论】:

        【解决方案3】:

        试试这个

          $schema = drupal_get_schema(NULL,TRUE);// Get List Of all tables. 
          ksort($schema);// Sort Ascending
          foreach($schema as $table => $value){
            print_r($table."\r\n");
          } 
        

        【讨论】:

          【解决方案4】:

          试试这段代码

          <?php
              $result = db_query("SHOW TABLES");
              foreach($result as $row){
                print_r($row);  
              }
          ?>
          

          $row 是一个对象。

          【讨论】:

            【解决方案5】:
            $result = db_query("SHOW TABLES");
            while($row = db_fetch_array($result)) {
              // Each table should be in $row
              // Same here for "SHOW COLUMNS FROM table_name;" from googletorp answer
            }
            

            【讨论】:

              猜你喜欢
              • 2013-10-16
              • 1970-01-01
              • 1970-01-01
              • 2011-06-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多