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';

Add new comment