How to select a users specific info from a MySQL database?
I was wondering how can I have my users pick their favorite food from a category of foods in my MySQL database.
Will I need two different tables? If so what will my second table look like?
Here is my MySQL food table structure.
id | parent_id | f开发者_如何转开发ood | url
You'll need 3 tables in total:
- Food - holds food information
- Users - holds users information
- Users_Food - holds user id + food id (and maybe a ranking)
You should probably read up on database normalization.
You'd need to make a second table:
user_id | food_id
Make them both primary. Then you can use JOIN's to select the food:
SELECT f.food, f.url
FROM user_food AS u
INNER JOIN food AS f ON (f.id = u.food_id)
WHERE u.user_id = {USER_ID}
This will give you a list of all favorite foods set by the user.
精彩评论