Development

Getting MySQL table column names, types and more as JSON

How to get information in JSON format about a MySQL table.

INFORMATION_SCHEMA has information about the metadata of the database structure such as column types, defaults, lenghts and keys.

To view all the available information returned for a table execute the following with your database & table name added.

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database'
  AND TABLE_NAME = 'table';

Now to build an array for the JSON output using PHP:

$database = 'the_db_name';
$table = 'test_table';

$output_array = array();

$select = $db->prepare("SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_DEFAULT, DATA_TYPE, IS_NULLABLE, COLUMN_KEY FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?;");
$select->execute([$database, $table]);

$cols = 0;
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
    $column = $row['COLUMN_NAME'];
    $type = $row['COLUMN_TYPE'];
    $type_short = $row['DATA_TYPE'];
    $default = $row['COLUMN_DEFAULT'];
    $is_nullable = $row['IS_NULLABLE'];
    $column_key = $row['COLUMN_KEY'];
    $output_array[] = array('column' => $column, 'type' => $type, 'type_short' => $type_short, 'default' => $default, 'is_nullable' => $is_nullable, 'key' => $column_key);
    $cols++;
}

$output_array = array_merge(array('database' => $database, 'table' => $table, 'columns' => $cols), $output_array);

To view output:

echo json_encode($output_array);

Here is a sample response:

{
  "database": "store",
  "table": "items_sample",
  "columns": 4,
  "0": {
    "column": "id",
    "type": "int(11)",
    "type_short": "int",
    "default": null,
    "is_nullable": "NO",
    "key": "PRI"
  },
  "1": {
    "column": "item",
    "type": "varchar(255)",
    "type_short": "varchar",
    "default": null,
    "is_nullable": "NO",
    "key": "MUL"
  },
  "2": {
    "column": "quantity",
    "type": "int(11)",
    "type_short": "int",
    "default": "0",
    "is_nullable": "NO",
    "key": ""
  },
  "3": {
    "column": "sold",
    "type": "int(11)",
    "type_short": "int",
    "default": "0",
    "is_nullable": "NO",
    "key": ""
  }
}

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago