我想将数据库中的表列表放入数组中,并将特定表的列名放入 Drupal 中的数组中。请提及 Drupal 中的查询。谢谢
如果您想要生成的结构不是数据库中实际的内容,而是您已激活的模块如何定义它的结构,您可以为激活的模块调用
hook_schema
。实际上有一个API调用,所以你所要做的就是调用 drupal_get_schema
这是一种获取信息的“简单”方法,但它不会接触数据库,因此任何使用 SQL 手动创建的表、不是来自 Drupal 的表或使用原始 SQL 进行的更改都将无效不会被发现。然而,在 99.9% 的情况下它都是准确的。 SQL:
SHOW TABLES;
SHOW COLUMNS FROM table_name;
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.
$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");
}
<?php
$result = db_query("SHOW TABLES");
foreach($result as $row){
print_r($row);
}
?>
$row
是一个对象。
$names = \Drupal::database()->schema()->findTables('%');