DB dilemma

Discussion in 'PHP' started by roice, Aug 23, 2012.

  1. #1
    Hello,

    I have USERS table (id, name, email, phone, lastUpdate, etc...)
    I want to add permissions to each user
    what will be better - to add fields to USERS table
    or
    to create new table USERS_permissions that will include id, userID, permmission1, permmission2, etc...
    ?

    Roi.
     
    roice, Aug 23, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    I would do something like the second one but slightly different.

    I would have USERS_PERMISSION with fields id, userId,permissionId and a table called PERMISSIONS with the fields id,name

    then populate something like this

    Permissions

    id, name
    1 , view records
    2, edit records
    3, delete records

    user_permissions
    id, userId, permissionId
    1, 1, 1
    2, 1, 2
    3, 1, 3
    4, 2, 1
    5, 2, 2
    6, 3, 1

    so above user 1 would be able to view edit and delete user 2 view and edit and user 3 just view records
     
    plussy, Aug 23, 2012 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, that is good if your lots of permissions.

    but if you have only couple, is it matter which way to use? the second way requeir from me to do 2 queries every time - the first one is for getting the user ID and the second is to located his permission...
     
    roice, Aug 23, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    well the result will be the same. however what if you end up creating more permissions in the future then you will have to change the db structure which is not ideal.

    also you can combine everything into one query.

    you could do something like this.
    (not tested)
    
    
    $sql = 'SELECT * FROM `users`,`usersPermission` WHERE `users`.`id`='.$_SESSION['userId'].' AND `usersPermission`.`userId`=`users`.`id`';
    
    
    PHP:
     
    plussy, Aug 23, 2012 IP
  5. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The above example is a query that merges two tables together and gets one result. If you took that line and pasted it into phpmyadmin and had true variables filled in you will see the results you get from it. While the above example is a good way to do it, you could however break that into two queries.

    First check for the user and make sure the id matches and result out with a mysql_num_rows() to make sure you are at least getting 1 row returned, and then inside the if statement after that (with a positive results $num == 1) then you could simple check for the other portion of that statement - the user permissions.

    But the above example is a good way to start using statements for joined commands
     
    InstaCoders, Aug 25, 2012 IP
  6. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks
    .........
     
    roice, Aug 26, 2012 IP