Problem When Serializing Array With Multiline Text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dawnerd

    Problem When Serializing Array With Multiline Text

    Hello everyone.

    I have a question, or problem if you will, that I'm sure someone knows
    the answer to. I have a database that stores information on a given
    user. The information is stored in a serialized array. This works
    flawlessly when using only single line text. When I tried to store
    multiline text, the problem arose.

    When the serialized data is deserialized, the array breaks. Any
    suggestions?

  • Jerry Stuckle

    #2
    Re: Problem When Serializing Array With Multiline Text

    dawnerd wrote:
    Hello everyone.
    >
    I have a question, or problem if you will, that I'm sure someone knows
    the answer to. I have a database that stores information on a given
    user. The information is stored in a serialized array. This works
    flawlessly when using only single line text. When I tried to store
    multiline text, the problem arose.
    >
    When the serialized data is deserialized, the array breaks. Any
    suggestions?
    >
    I don't even try to to store serialized data in a database. Rather, I
    create a database which reflects the data being used.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Bocah Sableng

      #3
      Re: Problem When Serializing Array With Multiline Text

      On Apr 30, 6:20 am, dawnerd <dawn...@gmail. comwrote:
      Hello everyone.
      >
      I have a question, or problem if you will, that I'm sure someone knows
      the answer to. I have a database that stores information on a given
      user. The information is stored in a serialized array. This works
      flawlessly when using only single line text. When I tried to store
      multiline text, the problem arose.
      >
      When the serialized data is deserialized, the array breaks. Any
      suggestions?
      I test (on windows using CLI):

      $a[] = "username";
      $a[] = 15;
      $a[] = "Not Found Street\n12345\n Nowhere City";
      $a[] = "Not Found Street\r\n12345 \r\nNowhere City";
      $k = serialize($a);
      $u = unserialize($k) ;
      var_dump($a);
      var_dump($k);
      var_dump($u);

      Seems fine for me.

      Please show us:
      1. The content of the serialized variable before it stored to
      database?
      2. The content of the deserialized variable picked from database?
      3. Field type you use to store serialized data?

      Comment

      • Chung Leong

        #4
        Re: Problem When Serializing Array With Multiline Text

        On Apr 30, 1:20 am, dawnerd <dawn...@gmail. comwrote:
        Hello everyone.
        >
        I have a question, or problem if you will, that I'm sure someone knows
        the answer to. I have a database that stores information on a given
        user. The information is stored in a serialized array. This works
        flawlessly when using only single line text. When I tried to store
        multiline text, the problem arose.
        >
        When the serialized data is deserialized, the array breaks. Any
        suggestions?
        Compare the length of the serialized string before and after. I bet
        you it's a CRLF vs CR issue. The lengths of strings are stored in the
        serialization data. If it loses or gains a character upon retrieval,
        then unserial() won't work.

        Comment

        • dawnerd

          #5
          Re: Problem When Serializing Array With Multiline Text

          On Apr 29, 7:48 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          dawnerd wrote:
          Hello everyone.
          >
          I have a question, or problem if you will, that I'm sure someone knows
          the answer to. I have a database that stores information on a given
          user. The information is stored in a serialized array. This works
          flawlessly when using only single line text. When I tried to store
          multiline text, the problem arose.
          >
          When the serialized data is deserialized, the array breaks. Any
          suggestions?
          >
          I don't even try to to store serialized data in a database. Rather, I
          create a database which reflects the data being used.
          >
          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstuck...@attgl obal.net
          =============== ===

          I would use a database just for the information, but the inforation
          being stored is site owner defined. They can add or remove fields as
          needed. I will post some more info on the problem when I get off work.

          Comment

          • Mike P2

            #6
            Re: Problem When Serializing Array With Multiline Text

            Do you use the relevant real_escape_str ing() function on the
            serialized data before inserting into the database? It does escape
            newlines, which I have never seen being a problem anyway, but it is
            good practice nonetheless. If you are just using addslashes(), it's
            not escaping newlines.

            -Mike PII

            Comment

            • Toby A Inkster

              #7
              Re: Problem When Serializing Array With Multiline Text

              dawnerd wrote:
              I would use a database just for the information, but the inforation
              being stored is site owner defined. They can add or remove fields as
              needed. I will post some more info on the problem when I get off work.
              The problem is that it will knacker your query time. Compare for example:

              Surname* Forename(s)* Organisation Nationality
              -------------------------------------------------------------------
              Gates Bill Microsoft US
              Jobs Steve Apple, Inc US
              Torvalds Linus Linux Foundation FI

              versus:

              ID* Object
              -------------------------------------------------------------
              1 array('sn'=>'Ga tes', 'fn'=>'Gates', 'o'=>'Microsoft ', 'co'=>'US')
              2 array('sn'=>'Jo bs', 'fn'=>'Steve', 'o'=>'Apple, Inc', 'co'=>'US')
              3 array('sn'=>'To rvalds', 'fn'=>'Linus', 'o'=>'Linux Foundation', 'co'=>FI')

              (An asterisk represents a primary key column. Note I'm using the case
              where you'd never want to store more than one person with the same surname
              and forename, which is arguably not very realistic.)

              Now, say you want to store Rasmus Lerdorf of Yahoo. In the first model,
              you'd just insert

              'Lerdorf', 'Rasmus', 'Yahoo', 'GL'

              And the database would reject it if there was already a Rasmus Lerdorf in
              the database, as the Surname and Forename columns are the primary key.

              In the latter, there would be no check for duplicates. If you wanted to
              check for duplicates, your code would need to loop through the entire
              table, read and parse each row to find duplicates, and only insert the
              data when there is no duplicate. This might be fine for a table of 3
              records, but when you have thousands, then it will cripple your
              application's speed.

              A search for all people with surname Gates who work at Microsoft would
              similarly require your application to loop through each row and parse the
              serialised object.

              If you really do need to be able to define additional fields of data on
              the fly, then you can easily do so through an "attributes " table. e.g.

              [People]
              Surname* Forename(s)* Organisation Nationality
              -------------------------------------------------------------------
              Gates Bill Microsoft US
              Jobs Steve Apple, Inc US
              Torvalds Linus Linux Foundation FI

              [Attributes]
              Surname* Forename(s)* Attribute Value
              ------------------------------------------------------
              Gates Bill hair-colour Brown
              Gates Bill favourite-os Windows Vista
              Jobs Steve favourite-os Mac OS X (Leopard)
              Torvalds Linus favourite-os Linux

              This method should give you complete freedom to define new fields in the
              Attributes table, while keeping the core fields in the People table. Easy.

              You can even do complex joins to figure out, say, a list of people with
              brown hair who like Windows Vista.

              SELECT forename||' '||surname
              FROM People p
              INNER JOIN Attributes a1
              ON al.surname=p.su rname AND a1.forename=p.f orename
              INNER JOIN Attributes a2
              ON a2.surname=p.su rname AND a2.forename=p.f orename
              WHERE (a1.attribute=' hair-colour' AND a1.value='brown ')
              AND (a2.attribute=' favourite-os' AND a2.value='Windo ws Vista');

              which should return one row:

              Bill Gates

              (and is actually a remarkably accurate list of all people with brown hair
              who like Windows Vista.)

              --
              Toby A Inkster BSc (Hons) ARCS
              Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

              Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

              Comment

              Working...