By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Problem When Serializing Array With Multiline Text

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.