title: Determine name of table referenced by a foreign key from another table using PHP?
body: <p>Just learning mysql and php and having to create a test project for my exam.</p>
  <p>How do I determine the name of a table (target) a foreign key from another table (source) references, and how would I go about that with PHP?</p>
  <p>I have been googling this as well as browsing SO for it for hours, but have not found a working solution.</p>
  <p>Edit: I see I need to be more precise. I want to look at a foreign key column definition in table A and find out which other table in my database it references, all this without knowing the referenced table's name. I also want this to work when table A is empty. So I guess I somehow need to access the definition / description (scheme?) of table A, look at what is stored there for the foreign key column in question and determine the target table that way.</p>
  <p>Example:</p>
  <p>I have a table &quot;products&quot; (source) and a table &quot;manufacturers&quot; (target).</p>
  <p>&quot;products&quot; has a column &quot;manufacturer&quot; which is a foreign key referencing &quot;manufacturers&quot;.</p>
  <p>Now I want to create some php code that looks at the column &quot;manufacturers&quot; of table &quot;products&quot; and finds out that foreign keys stored in this column would reference table &quot;manufacturers&quot;. All this without the php knowing the referenced table in advance. This should also work if the table &quot;products&quot; is empty, i.e. does not contain any foreign key values.</p>
  <p>So any SQL query already requiring me to enter the referenced table's name seems pointless to me.</p>
  <p>I tried</p>
  <pre><code>CREATE TABLE manufacturers (
      id INTEGER PRIMARY KEY,
      name VARCHAR(100)
      );
  
  CREATE TABLE products (
      id INTEGER PRIMARY KEY,
      name VARCHAR(100),
      manufacturer INTEGER
      );
  
  ALTER TABLE products ADD FOREIGN KEY(manufacturer) REFERENCES manufacturers(id);
  
  SELECT TABLE_NAME,
         COLUMN_NAME,
         REFERENCED_TABLE_NAME
  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = &quot;products&quot;
    AND REFERENCED_TABLE_NAME IS NOT NULL;
  </code></pre>
  <p>But the result was empty.</p>
  <p>Edit 2:</p>
  <p>I found out that INFORMATION_SCHEMA.KEY_COLUMN_USAGE doesn't exist in my entire sql installation. Or at least I can see it nowhere.</p>
  <p>Edit 3:</p>
  <p>I found my mistake. Instead of &quot;DATABASE()&quot;, I have to specify my actual database name.</p>
  <p>Thanks for all the help. :)</p>