How can I delete something from an array in MySql?

Discussion in 'PHP' started by RyanDoubleyou, Jun 16, 2008.

  1. #1
    Hello, I am trying to make a friends list for my website http://tackypenguin.com. I was thinking that I would insert a row into the database for each user. This row would have all of the person's friends listed in an array. The problem is, I don't know how I would delete a friend if someone wanted to delete it. Anyone have an idea? I have Googled, but I couldn't find anything about actually deleting part of the array.
     
    RyanDoubleyou, Jun 16, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I hope you are using usto-incrementing IDs for each new records added to the databaase. If you are, just run the following SQL:
    
    $sql="DELETE FROM table_name WHERE id='$id'";
    
    PHP:
    where $id is the id of the record!
     
    rohan_shenoy, Jun 17, 2008 IP
  3. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No. I was wondering if i could get, say user1, user2, user3, and user4, and insert them into one row that belongs to user 5. Have the all in one array, and explode them by "||" to get the friends listed.
     
    RyanDoubleyou, Jun 17, 2008 IP
  4. hanushh

    hanushh Peon

    Messages:
    198
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    y dont u use phpmyadmin
     
    hanushh, Jun 17, 2008 IP
  5. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hes not saying that he wants to delete them right now, he means when a user doesnt want someone on their friends list, that they can click delete and it deletes them from the array,

    i dont know how to do this, but i do get your question.
     
    X.Homer.X, Jun 17, 2008 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I understand the question, but your 'design' is wrong. You should have a row for each 'friend' of a user, so if user 2 is friends with 4, 7, 8 and 12, you would have four rows:

    Something like:
    RowID|UserID|FriendID
    1|2|4
    2|2|7
    3|2|8
    4|2|12

    Then you just delete the row that contains the particular user ID and the particular friend ID.
     
    TwistMyArm, Jun 17, 2008 IP
  7. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay, thanks TwistMyArm, I think thats what I needed. And yes X.homer.X thats what I ment.
     
    RyanDoubleyou, Jun 17, 2008 IP
  8. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    But how many rows will you make in such way? Lets say you have 1000 users and each of them have 100 friends, just imagine how many rows would that take up. Also it will affect the speed of the application. Ryan's first approach about storing it using delimiters is more efficient than a new rows for every friend I feel.
     
    rohan_shenoy, Jun 17, 2008 IP
  9. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Do you know how I would do it though. I had an idea, but how would I remove a friend if a user wanted to remove it?
     
    RyanDoubleyou, Jun 17, 2008 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Firstly, what's 100,000 rows? Nothing. If it's indexed correctly you won't have a problem and the size is negligible if you use sane fields.

    Secondly, and, as far as I'm concerned, more importantly: doing it with delimiters is wrong. Your row should contain a single 'fact'... all your asking for is more trouble in the long run. You know that at some point in the future the question will be asked "OK, so how many people have me as their friend?" You wanna do string searches, you go ahead.
     
    TwistMyArm, Jun 18, 2008 IP
  11. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    But, I dont want to do that. Is there a way to delete from an array in a row in MySql? Thats my question.
     
    RyanDoubleyou, Jun 18, 2008 IP
  12. Plazey

    Plazey Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #12
    $array = explode("|" $array);

    foreach ($array as $tmparray) {
    if ($tmparray != $arraytodelete) {
    $newarray .= $tmparray . "|";
    }
    }

    $array = $newarray;


    deleted...


    [edit]

    Forgot to add the sql.

    $query = "UPDATE $UPDATE $tableName SET $columnName = $newValue WHERE $columnName = '$someValue'";
     
    Plazey, Jun 18, 2008 IP
  13. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #13
    Hey I noticed on your web site also that you have it saying that your users need to register to your web site again. This is not the most professional approach to doing this, 1 easy way is to write a program to gather all users passwords and turn them into a hash if that is what you are trying to do.

    Just some advice because I have created a hole friends network before.
     
    ryandanielt, Jun 18, 2008 IP
  14. Plazey

    Plazey Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #14
    Bye the way. I just had to say, your friend has a pretty nice looking site. Just one thing cought my eye as ugly. [​IMG]
    You might wanna tell him to change the black lines to a light blue or even white. And also center the text between the lines.
     
    Plazey, Jun 18, 2008 IP
  15. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #15
    Personally I think that there is many ways to make this look alot better then is does now. Some time and TLC really will need to be put into this site and I think it might have some potential.
     
    ryandanielt, Jun 18, 2008 IP
  16. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks from everyone. I had it to where it updated to md5 when the user logged in, but there was en error in it, and somehow it deleted all of my users. I have it green on the sidebar now, and when you login with your account, you can set a style, and it will be that style every time you login.
     
    RyanDoubleyou, Jun 20, 2008 IP