Rustage Posted August 4, 2009 Share Posted August 4, 2009 Couldnt think where to post this, but I suppose you could class it as php help.My problem is, im making a community website for gamers and I want to allow the members to add friends to their profiles, sort of like you would on facebook, myspace etc. But I can't think of a simple way to store peoples friends, I could have fields in the mysql such as "friend1" "friend2" etc. but i think that will be a waste of time really as some people might have 1,000 friends or even 10,000.Just wondering if anyone has any other ideas? Im not the best at php and mysql as im pretty much learning it as I go but thought id ask you guys if you have any ideas.Thanks very much, Rustage Link to comment Share on other sites More sharing options...
jeffman Posted August 4, 2009 Share Posted August 4, 2009 You read a lot about not saving comma-separated lists inside a database field, but it might well be your answer. PHP's str_getcsv might even help. http://www.php.net/manual/en/function.str-getcsv.php Link to comment Share on other sites More sharing options...
Rustage Posted August 4, 2009 Author Share Posted August 4, 2009 Cheers, ill check it out.. from a glance looks like it might work! Link to comment Share on other sites More sharing options...
clonetrooper9494 Posted August 4, 2009 Share Posted August 4, 2009 Would there be any (dis)advantages to having a table called friends, where col1 would be the userID of the user who sent the request, and col2 would be the userID of the receiver of the request? I am thinking about doing this, but I am not sure... Link to comment Share on other sites More sharing options...
justsomeguy Posted August 4, 2009 Share Posted August 4, 2009 That's the way it would normally be done, that allows for more flexibility (e.g. find all users who have me as a friend). Link to comment Share on other sites More sharing options...
jeffman Posted August 4, 2009 Share Posted August 4, 2009 So if Bob1234 has 1000 friends, there would be 1000 rows where "Bob1234" fills the userID column? This is the most efficient technique? Just confirming, I think. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 4, 2009 Share Posted August 4, 2009 It's not that bad, if each user ID is a 32-bit number then you need 8 bytes of storage to store one relationship. So it would take 8KB to indicate that Bob has 1000 friends. The main benefit is that it's a lot easier to search through that table if you're looking up relationships than it is to search through a bunch of CSV strings. Assume you have a user with ID 100, for example. You could just find all records that contain "100" somewhere in the string, but you're going to find "1100", "1000", etc. So in order to find all fields that have user 100 as a friend you need to look for these conditions:field contains only "100" and nothing elsefield starts with "100,"field ends with ",100"field contains ",100,"That query is going to take a lot more time than it will to look up an index on the relationship table.That's not even mentioning what you need to do if a user decides they want to remove one of their 1000 friends. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.