Get foreign keys using PHP

I am creating a class in PHP where I only have the Table Name parameter and selects all columns and values. But I do not know how to get the values ​​of columns with foreign keys.
 This is because I do not know which table it refers to.

I need a way that I can get a foreign key with PHP or SQL from a known table?

+3
source share
2 answers

In MySql, you can query information_schemato get meta-information about the database.

SELECT
  TABLE_NAME AS `table_name`,
  COLUMN_NAME AS `column_name`,
  REFERENCED_COLUMN_NAME AS `referenced_column_name`,
  REFERENCED_TABLE_NAME AS `referenced_table_name`
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
  AND REFERENCED_TABLE_SCHEMA = DATABASE()

From: https://github.com/troelskn/pdoext/blob/master/lib/pdoext/connection.inc.php#L413 (Specifically, a function loadKeys)

+5
source

MySQL:

SHOW CREATE TABLE mytable
+1

All Articles