473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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?

Apr 29 '07 #1
6 2969
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.
js*******@attglobal.net
==================
Apr 30 '07 #2
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\nNowhere 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?

Apr 30 '07 #3
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.

Apr 30 '07 #4
On Apr 29, 7:48 pm, Jerry Stuckle <jstuck...@attglobal.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...@attglobal.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.

Apr 30 '07 #5
Do you use the relevant real_escape_string() 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

Apr 30 '07 #6
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'=>'Gates', 'fn'=>'Gates', 'o'=>'Microsoft', 'co'=>'US')
2 array('sn'=>'Jobs', 'fn'=>'Steve', 'o'=>'Apple, Inc', 'co'=>'US')
3 array('sn'=>'Torvalds', '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.surname AND a1.forename=p.forename
INNER JOIN Attributes a2
ON a2.surname=p.surname AND a2.forename=p.forename
WHERE (a1.attribute='hair-colour' AND a1.value='brown')
AND (a2.attribute='favourite-os' AND a2.value='Windows 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
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
7
by: Joel Finkel | last post by:
Folks, I have a form that has several TextBoxes, some of which have the TextMode set to MultiLine. Each is pre-loaded with data from a database. The user is allowed to modify each entry. The...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
8
by: David | last post by:
This is something I had never seen before. On an aspx page, upon pressing a link button for which I have an event handler in the code behind, the screen shows nothing but a line that says "true"...
3
by: Antonio Lopez Arredondo | last post by:
hi all again..... I have an ASP.NET application that has a multiline textbox and a single button. the user should be able to write inside the many paragraphs in the multiline textbox and then...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.