mysql 如何在Drupal中从数据库中获取表名列表

yws3nbqq  于 2023-10-15  发布在  Mysql
关注(0)|答案(6)|浏览(116)

我想从数据库中获取表列表到数组中,并将特定表的列名放入Drupal数组中。请注意Drupal中的查询。谢谢

nnvyjq4y

nnvyjq4y1#

如果您不想生成数据库中实际存在的结构,而是想生成已激活的模块如何定义它的结构,那么可以为已激活的模块调用hook_schema。它实际上有一个API调用,因此您只需调用drupal_get_schema
这是一种获取信息的简单方法,但它不会触及数据库,因此不会找到任何使用SQL手动创建的表,或不是来自Drupal的表,或使用原始SQL进行的更改。在99.9%的情况下,它是准确的。
SQL:

SHOW TABLES;
SHOW COLUMNS FROM table_name;
t3psigkw

t3psigkw2#

试试这个

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.
muk1a3rh

muk1a3rh3#

试试这个

$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");
  }
v1uwarro

v1uwarro4#

试试这段代码

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

$row是一个对象。

dhxwm5r4

dhxwm5r45#

$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
}
ajsxfq5m

ajsxfq5m6#

Drupal 9|| 10^您可以执行以下操作:

$names = \Drupal::database()->schema()->findTables('%');

相关问题