I want to save an array in my database friends column. Actually I want to create friend request system. My method is a little bit different. I want to have two columns for this system.

| Name   | Friends                   |  Requests                     |
| Name1  | array(person1,person2..)  |  array(request1, request2..)  |
| Name2  | array(person1,person2..)  |  array(request1, request2..)  |

When name1 adds name2 the name1 name must be put in requests of name2 when name2 accepts the requests the name1 will be removed from name2 requests and added to both name1 and name2 friends.

Now the problem is that I have no idea how to put an array into a column cell and how to get the name when needed from it by a select query. I have just worked with simple queries a lot but not this kind of thing.

  • It would be best if you showed what you have tried so far and why it doesn't work. Since this is a DBA site it would be appropriate to add tags for your DBMS. – Michael Green Aug 24 '14 at 11:30
  • This is probably a Stack Overflow question as it's about PHP coding rather than the actual database. – Mark Sinkinson Aug 24 '14 at 13:02

You should re-evaluate your database design. A relational database is not designed to store arrays in columns. Actually, elements in arrays are "columns" itself. So, what you're trying to do is store multiple columns in a single column...

I'd recommend two tables: Persons (PersonId, Name,...) Friends (FriendsId, PersonId, FriendId, Accepted), where both, PersonId and FriendId have a foreign key to Persons.PersonId. The column Accepted will be Zero as long as it is not accepted. When accepted, just change it to 1. Therewith, you don't have to move rows arond and take care that there are no inconsistent data (or even missing data).

|improve this answer|||||
  • But i am thinking of this way :v $serialized_array = serialize($This); $unserialized_array = unserialize($serialized_array); – fafasdfsadf Aug 24 '14 at 9:02
  • 3
    You're mixing application layer and databse layer. The PHP syntax you're thinking of has nothing to do with a relational model. The way you're trying to store the data violates the 1st Normal Form and is a nightmare to maintain at the database level. However, the communication between the database ad the application could still use the "array concept". But the table design should not. – Lmu92 Aug 24 '14 at 11:26
  • Well, this is not 100% true as PostgreSQL has array types, for example. Other than that, I agree. – dezso Aug 24 '14 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.