Jump to content

Store multiple usernames in the same field as a String?


beennn

Recommended Posts

How can i store multiple usernames in the same field as a string on a database?Each user has a field for freinds and when they click 'add as friend' on someones profile, id like it to add to their friends field so i can use it when it comes to sending message.

Link to comment
Share on other sites

Try something like thisUPDATE table SET username_friends = CONCAT(username_friends," . $friend . ") WHERE id = " . $username . "

Link to comment
Share on other sites

For things like that it's best to use another table. Have a table called "friends", with columns for "user_id" and "friend_id". If you have a record where user_id is 1, and friend_id is 2, then that means that user 2 is the friend of user 1. That's a many-to-many relationship, where one user can have many friends, and one user can be friends to many people. For that type of relationship it's best to use a lookup table that stores the relationships. It becomes difficult and less efficient if you're trying to search through a single field that contains a comma-separated list of IDs.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...