Get table column names on Drupal 8 | Drupal 8

Get table column names on Drupal 8

Submitted by editor on Wed, 11/29/2017 - 10:36
Question

How to get database table column names on drupal 8 ?

To get database table columns names of a mysql database you can use "DESCRIBE my_table;" method.

Example: get columns of the table 'node'
$con = \Drupal\Core\Database\Database::getConnection();
$fields = $con->query("DESCRIBE `node`")->fetchAll();
var_dump($fields);

 

For the new database versions you can use INFORMATION_SCHEMA (Not tested) Like:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';

Comments

Radwan (not verified)

Wed, 06/06/2018 - 00:39

We can use drush as well.
drush sql-query "describe node"

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.