1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP data serialization in mysql

Discussion in 'PHP' started by News Updates, Jun 20, 2014.

  1. #1
    Hello guys, I need a little info.

    I'm trying to save data in serialized form. I'm getting two different results. Can anyone please tell me whats the 'technical' difference between both results?
    SEMrush
    [​IMG]
     
    Solved! View solution.
    News Updates, Jun 20, 2014 IP
    SEMrush
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,731
    Likes Received:
    26
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Explain to us why you are saving the data serialized? and what should the data contain? with this only the length is differend!
     
    EricBruggema, Jun 20, 2014 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,731
    Likes Received:
    26
    Best Answers:
    13
    Trophy Points:
    175
    #3
    Please don't use serialize for this purpose, use the database abilities for it! :)
    Create a table for followers with 2 (or more) fields (user_id, follow_userid) that's way better..
    That way you always have correct results...
     
    EricBruggema, Jun 20, 2014 IP
    News Updates likes this.
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    A good comment on PHP seralize documentation explains what you need:

    
    Anatomy of a serialize()'ed value:
    
    String
    s:size:value;
    
    Integer
    i:value;
    
    Boolean
    b:value; (does not store "true" or "false", does store '1' or '0')
    
    Null
    N;
    
    Array
    a:size:{key definition;value definition;(repeated per element)}
    
    Object
    O:strlen(object name):eek:bject name:eek:bject size:{s:strlen(property name):property name:property definition;(repeated per property)}
    
    String values are always in double quotes
    Array keys are always integers or strings
        "null => 'value'" equates to 's:0:"";s:5:"value";',
        "true => 'value'" equates to 'i:1;s:5:"value";',
        "false => 'value'" equates to 'i:0;s:5:"value";',
        "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an
        array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";',
        and
        attempting to use an object as a key will result in the same behavior as using an array will.
    
    Code (markup):
    Source
     
    ThePHPMaster, Jun 21, 2014 IP
    News Updates likes this.
  5. #5
    Serializing the data is _a_ solution, but it's not the _right_ solution. It rarely ever is. The better solution (perhaps the right one), is to create separate tables, preferably with foreign keys as well, and put them in there - as @EricBruggema said in the post above.
    This is the whole point of relational databases - you have a table with a unique identifier, which you then can use to connect other data and create new relationships - for instance by adding a user_followers and user_following table (strictly speaking, you only really need one, which is user_relationship - where you have user_id, connected_user_id, following, follower - that's basically all you need. Then you create a new entry in that table every time there's a new connection between user_id and connected_user_id - and update it if it changes.
     
    PoPSiCLe, Jun 21, 2014 IP
  6. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #6
    In the first table where you have s:1, you have an integer and a string, while in the second table you have 2 integer values
    Make sure you will insert your data like this: $data = array (0,3) instead of $data = array( 0, '3'); so that php will not attempt to index that as a string.
     
    hip_hop_x, Jun 22, 2014 IP
    News Updates likes this.
  7. News Updates

    News Updates Member

    Messages:
    490
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #7
    Hi guys, sorry for late follow up. I've realized that serialization is not convenient. I was thinking to take serialization to json for iphone app. Now I'm using simple arrays. Thanks for clarification.
     
    News Updates, Jun 24, 2014 IP