Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

How to Handle a Many-to-Many Relationship with PHP and MySQL

By Tony Marston
2005-04-06


Summary

I first designed this solution to the many-to-many problem several years ago while developing in a different language. It was an interesting exercise to convert it to PHP and MySQL.

In my long career I have also encountered variations of this problem. In this solution table 'X' contains nothing but the foreign key fields which link up to the two related tables, but this can be made more complicated by the following:

* There may be additional fields on table 'X', which means that the database update routine will have to deal with more than simply choosing between an insert or a delete.

* A more complicated version is where table 'X' can contain multiple occurrences for each combination of 'a_id' and 'b_id'. The example I have encountered several times is where each entry covers a different date range causing only one to be regarded as 'current' on any particular date, thus providing a history of values over a period of time.

However, I shall leave the solutions to these additional problems to a later date.

Tutorial Pages:
» Intended Audience
» Introduction
» Database schema
» Form design
» Summary


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1