How to store friend ids?

Codes here !

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 91
Joined: Sat Aug 13, 2011 7:20 pm

Tue Jul 31, 2012 4:35 am


I'm learning basic social networking off the Webintersect demo, in which a user's friend ids are stored in a text column in the format 3, 6, 99, 44 etc, so this is how I've set things up.

To add a friend, the whole array gets loaded into php and the id is added, then the new array string is saved back in mysql. However, I now see that it would be a problem if someone gets added when another transaction is in process.

What is a better way to store friend ids? A simple table with two columns - `your_id` and `friend_id`, so there's a new row for each of your friends?

Thanks for your help.

php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Wed Aug 01, 2012 11:27 pm

It's better relational design to store the friends as a separate table as you mention. It means that the database can use foreign keys to ensure that your friend really exists ( and not an imaginary friend ) as well as that the friend id your trying to store is actually a number.

Post Reply