I am creating a class which, takes a table from a database, and displays it to a web-page, with as much functionality as possible. One of the things I would like to support,
Much can be retrieved from MySQL's information_schema, foreign keys included, as pointed out by dev-null-dweller.
SELECT * FROM information_schema.table_constraints
WHERE table_schema = 'dbname' AND table_name='mytable';
Instead of dbname use the function SCHEMA() to set the name of the database in USE.
As pointed out by Dan Grossman, the command
SHOW CREATE TABLE `yourtablename`
can be used basically get an SQL dump of the create table statement.
MySQL provides a SHOW KEYS command. As such you could theoretically get the FK if you know a lower cardinality threshold and have few other keys in the table.
SHOW KEYS FROM `address` WHERE Non_unique AND CARDINALITY > 10000
As the key's cardinality changes each time the internal database is changed, this is rather theoretical. See the cardinality change for instance with running ANALYZE TABLE.
It is useful to stick to a naming schema, such as foreigntablename_foreignfieldname. For example the field user_id in a table billing. Several ORMs of big Web Content Frameworks use this schema.