#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0

    Question How You Would INSERT ?


    Php Masters!

    I need to know how you do things the way you do things.
    Let us say, I am building a Social Network (SN).
    Let us say, my mysql db looks like this in structure:

    Tbl: users;
    Columns: id, username, email, friends.

    Example:

    id|username|email|friends
    0|uniqueideaman|uniqueideaman@**.com|catacaustic,bananaman,kicken

    Do you see, how I have crunched-up 3 usernames into a single cell ?
    Is this how you would do it to list my friends' usernames ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,436
    Rep Power
    652
    Probably not.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,244
    Rep Power
    4150
    Originally Posted by UniqueIdeaMan
    Do you see, how I have crunched-up 3 usernames into a single cell ?
    Is this how you would do it to list my friends' usernames ?
    No, learn about Database Normalization, specifically 4th normal form (4NF).

    Comments on this post

    • UniqueIdeaMan agrees : Thanks for the links.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0
    Originally Posted by gw1500se
    Probably not.
    So, how would you do it then ?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0
    Originally Posted by kicken
    No, learn about Database Normalization, specifically 4th normal form (4NF).
    Thanks for the links, Gave you a REP.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0
    Originally Posted by gw1500se
    Probably not.
    So, you think I should do it like this:

    Anyway, I was told by Kicken to check these out:
    https://en.wikipedia.org/wiki/Fourth_normal_form
    https://www.youtube.com/watch?v=UrYLYV7WSHM

    Did not bother with the vid as I'm on low bandwidth.

    Also, googled:
    https://www.google.com/search?q=what...hrome&ie=UTF-8

    And read:
    https://www.tutorialcup.com/dbms/fourth-normal-form.htm


    You think I should have a friends list where every member link is recorded in a three column table, the first being an auto_increment ID to keep all records sequential and unique, the 2nd ID is the User ID (UID) and the Friend ID is the FID.

    Tbl: Freinds
    ID is auto incremented.
    UID is User's ID.
    FID is Friend's ID.

    Columns
    ID | UID | FID
    1 | 3 | 24
    2 | 3 | 399
    3 | 55 | 24
    4 | 598 | 3
    5 | 598 | 55
    6 | 6000 | 598
    7 | 3 | 598
    8 | 24 | 55
    9 | 55 | 598

    So, you think apart from having the "users" tbl that lists the users during registration, I should have another tbl "friends" ?
    And you want me to populate the "freinds" tbl like shown above. Right ? Yes or no ?
    If "yes", then how would you db query to pull-up a user's friends ? Let's say you want to pull-up UID 5's friends.
    This is my try using PREP STMNT:

    PHP Code:
    $stmt mysqli_prepare($conn"SELECT FID FROM users WHERE UID = ? "); 

    @gw1500se & @kicken,
    In the above example you can see that ID's 1 & 2 have two entries for UID 3 who is linked to friend's FID 24 & 399 and is also friends with UID 598.
    It appears that I am having multiple links here. And therefore, I did not originally want to do it this way to save the db getting populated to much (too many entries) to save it from getting bogged down from doing too many tasks. Would not it be less resource using and less querying (hence less traffic to the db) if I do it the way I demonstrated in my original post ?
    Last edited by UniqueIdeaMan; January 8th, 2018 at 12:53 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,436
    Rep Power
    652
    You need to establish a many-to-many relationship in your DB (aka join table) between members and friends.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0
    Originally Posted by gw1500se
    You need to establish a many-to-many relationship in your DB (aka join table) between members and friends.
    Oh! I not sure if I quite follow you. So, how about you show me an example like I showed in my previous 2 posts ?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,436
    Rep Power
    652
    How about your learn about database design? Hopefully better than you learn about PHP.

    Comments on this post

    • UniqueIdeaMan agrees : Thanks for the db design link.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    738
    Rep Power
    0
    Originally Posted by gw1500se
    How about your learn about database design? Hopefully better than you learn about PHP.
    Thanks for the db design link. Gave you a REP.

IMN logo majestic logo threadwatch logo seochat tools logo