3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getListTableForeignKeysSQL($table, $database = null) { $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ". "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ". "FROM information_schema.key_column_usage k /*!50116 ". "INNER JOIN information_schema.referential_constraints c ON ". " c.constraint_name = k.constraint_name AND ". " c.table_name = '$table' */ WHERE k.table_name = '$table'"; $databaseNameSql = null === $database ? "'$database'" : 'DATABASE()'; $sql .= " AND k.table_schema = $databaseNameSql /*!50116 AND c.constraint_schema = $databaseNameSql */"; $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL"; return $sql; } echo getListTableForeignKeysSQL('AreaLayoutsUsingPresets'). "\n"; echo getListTableForeignKeysSQL('AreaLayoutsUsingPresets', 'concrete5db'). "\n";
based on HNcms
Output for 5.3.18 - 7.3.0rc6
SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ FROM information_schema.key_column_usage k /*!50116 INNER JOIN information_schema.referential_constraints c ON c.constraint_name = k.constraint_name AND c.table_name = 'AreaLayoutsUsingPresets' */ WHERE k.table_name = 'AreaLayoutsUsingPresets' AND k.table_schema = '' /*!50116 AND c.constraint_schema = '' */ AND k.`REFERENCED_COLUMN_NAME` is not NULL SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ FROM information_schema.key_column_usage k /*!50116 INNER JOIN information_schema.referential_constraints c ON c.constraint_name = k.constraint_name AND c.table_name = 'AreaLayoutsUsingPresets' */ WHERE k.table_name = 'AreaLayoutsUsingPresets' AND k.table_schema = DATABASE() /*!50116 AND c.constraint_schema = DATABASE() */ AND k.`REFERENCED_COLUMN_NAME` is not NULL