1

In my current project in PHP and MySQL, I have a database of user and a database of projects. I want to be able to assign projects to users so that they only have access to those that they have joined (by whatever means). Is there a more efficient way of doing this other than having some sort of array within a field of a user?

1 Answer 1

4

Create a table with two foreign keys: one pointing to the primary key of the user table and the other pointing to the primary key of the project table. This is the standard way of implementing so called m:n-relations (a user can have multiple projects and a project consists of multiple users) in relational databases. Read up on database normalization for reasons why.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. Do the foreign keys have to be called anything specific or are they just numbers?
Also, I'll be adding a sort of shoutbox and various other dynamic content on each project page - where is the best place to store all all of this?
The type and name of foreign keys is up to you, but they must have the same type as the primary key that they refer to and should be named such that it is clear to the reader which primary key they refer to.
Do not think in terms of pages when designing the database. Think in terms of entities, their attributes and relations to other entities. See Entity-relationship model for details.
After linking them, how do I correspond one to the other. For example, how do I change what projects a user is subscribed to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.