For years, I have wanted a way to programatically find foreign keys in MySQL tables using PHP. After a lot of thought, I have written Phind. Phind basically consists of one function getForeignKeys() whose only parameter is the table name. If that table has foreign keys, Phind returns a multidimensional array containing an index for the key as the first index, and the part of the key as the second index.
My ultimate goal is to use Phind to create a PHP CRUD generator written entirely in PHP. That’ll probably be a long way off, though. I just don’t have time to sit down and finish something like that right now.
The source code for phind is here: Simply rename the file from phind.txt to phind.php when you save it. You can also go here to see Phind in action on a table in a database on my server named ‘contacts.’ Eventually, I will add more examples to the documentation included with the phind.php file, but for now, it’s sparse. However, I’m sure there are enough PHP guys who have been looking for a way to make sense of what foreign keys are in a table and where exactly they are pointing to.
I hope this helps somebody.
Leave a Reply