473,507 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing serialized data in MySQL table

PI
Hi Guys,

I need some assistance with the following scenario please. I know this
might be more of a MySQL post than a PHP one, but it is likely some
developer has been here before and would be able to help out.

I have some serialized data that I would like to store in a MySQL
database. When viewed, the serialized data looks like this:

O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:18: \"\0cart
\0c_cart_owner\";s:5:\"owner\";s:22:\"\0cart\0c_ca rt_date_time\";N;s:
20:\"\0cart\0c_arr_contents\";a:1:{i:0;O:9:\"cart_ item\":5:{s:
25:\"\0cart_item\0c_cart_item_id\";s:3:\"162\";s:2 7:\"\0cart_item
\0c_cart_item_name\";s:10:\"30BWL15481\";s:34:\"\0 cart_item
\0c_cart_item_description\";s:23:\"BOWL,MINIDOT,PR ISM,POLY\";s:
31:\"\0cart_item\0c_cart_item_quantity\";i:1;s:20: \"\0cart_item
\0c_cart_id\";N;}}}

The problem is that I have not been able to successfully persist this
data in the database. The Schema I am currently using for the table
is:
+--------------+--------------+------+-----+---------+----------------
+
| Field | Type | Null | Key | Default | Extra
|
+--------------+--------------+------+-----+---------+----------------
+
| order_id | int(10) | NO | PRI | NULL | auto_increment
|
| order_number | varchar(255) | YES | | NULL |
|
| user_id | varchar(255) | YES | | NULL |
|
| product_data | longtext | YES | | NULL |
|
| session_data | varchar(255) | YES | | NULL |
|
| date | varchar(255) | YES | | NULL |
|
+--------------+--------------+

When I attempt to insert this data in the table using the following
query statement:

SQL query is is: INSERT INTO spares_order (order_number, user_id,
product_data, session_data, date) VALUES ('tlx-18954XM',
'i**@gmail.com', 'O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:
18:\"\0cart\0c_cart_owner\";s:5:\"owner\";s:22:\"\ 0cart
\0c_cart_date_time\";N;s:20:\"\0cart\0c_arr_conten ts\";a:1:{i:0;O:
9:\"cart_item\":5:{s:25:\"\0cart_item\0c_cart_item _id\";s:3:\"162\";s:
27:\"\0cart_item\0c_cart_item_name\";s:10:\"30BWL1 5481\";s:
34:\"\0cart_item\0c_cart_item_description\";s:
23:\"BOWL,MINIDOT,PRISM,POLY\";s:31:\"\0cart_item\ 0c_cart_item_quantity
\";i:1;s:20:\"\0cart_item\0c_cart_id\";N;}}}',
'0cae0dd4494d178a04244be03fff68cd', 'Wednesday 12th of November 2008
09:58:29 AM')

I have no error messages but the table column product_data which
ought to hold the serialized data holds only part of the complete
string as shown:

+----------------------+
| product_data |
+----------------------+
| O:4:"cart":4:{s:15:" |
+----------------------+
I understand this might have to do with certain characters within
that serialized data that needs to be escaped. I have attempted a
number of escape options but none seems to work for me. I would
appreciate some assistance with this please. Thanks a lot.
Nov 12 '08 #1
9 5478
PI wrote:
O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:18: \"\0cart
\0c_cart_owner\";s:5:\"owner\";s:22:\"\0cart\0c_ca rt_date_time\";N;s:
20:\"\0cart\0c_arr_contents\";a:1:{i:0;O:9:\"cart_ item\":5:{s:
25:\"\0cart_item\0c_cart_item_id\";s:3:\"162\";s:2 7:\"\0cart_item
\0c_cart_item_name\";s:10:\"30BWL15481\";s:34:\"\0 cart_item
\0c_cart_item_description\";s:23:\"BOWL,MINIDOT,PR ISM,POLY\";s:
31:\"\0cart_item\0c_cart_item_quantity\";i:1;s:20: \"\0cart_item
\0c_cart_id\";N;}}}

[...]

+----------------------+
| product_data |
+----------------------+
| O:4:"cart":4:{s:15:" |
+----------------------+
Did you try add_slashes()?
http://de.php.net/manual/en/function.addslashes.php
Sven Reuter
--
http://www.sReuter.net/
http://www.Auskennbert.de/
Nov 12 '08 #2
PI schreef:
Hi Guys,

I need some assistance with the following scenario please. I know this
might be more of a MySQL post than a PHP one, but it is likely some
developer has been here before and would be able to help out.

I have some serialized data that I would like to store in a MySQL
database. When viewed, the serialized data looks like this:

O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:18: \"\0cart
\0c_cart_owner\";s:5:\"owner\";s:22:\"\0cart\0c_ca rt_date_time\";N;s:
20:\"\0cart\0c_arr_contents\";a:1:{i:0;O:9:\"cart_ item\":5:{s:
25:\"\0cart_item\0c_cart_item_id\";s:3:\"162\";s:2 7:\"\0cart_item
\0c_cart_item_name\";s:10:\"30BWL15481\";s:34:\"\0 cart_item
\0c_cart_item_description\";s:23:\"BOWL,MINIDOT,PR ISM,POLY\";s:
31:\"\0cart_item\0c_cart_item_quantity\";i:1;s:20: \"\0cart_item
\0c_cart_id\";N;}}}

The problem is that I have not been able to successfully persist this
data in the database. The Schema I am currently using for the table
is:
+--------------+--------------+------+-----+---------+----------------
+
| Field | Type | Null | Key | Default | Extra
|
+--------------+--------------+------+-----+---------+----------------
+
| order_id | int(10) | NO | PRI | NULL | auto_increment
|
| order_number | varchar(255) | YES | | NULL |
|
| user_id | varchar(255) | YES | | NULL |
|
| product_data | longtext | YES | | NULL |
|
| session_data | varchar(255) | YES | | NULL |
|
| date | varchar(255) | YES | | NULL |
|
+--------------+--------------+

When I attempt to insert this data in the table using the following
query statement:

SQL query is is: INSERT INTO spares_order (order_number, user_id,
product_data, session_data, date) VALUES ('tlx-18954XM',
'i**@gmail.com', 'O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:
18:\"\0cart\0c_cart_owner\";s:5:\"owner\";s:22:\"\ 0cart
\0c_cart_date_time\";N;s:20:\"\0cart\0c_arr_conten ts\";a:1:{i:0;O:
9:\"cart_item\":5:{s:25:\"\0cart_item\0c_cart_item _id\";s:3:\"162\";s:
27:\"\0cart_item\0c_cart_item_name\";s:10:\"30BWL1 5481\";s:
34:\"\0cart_item\0c_cart_item_description\";s:
23:\"BOWL,MINIDOT,PRISM,POLY\";s:31:\"\0cart_item\ 0c_cart_item_quantity
\";i:1;s:20:\"\0cart_item\0c_cart_id\";N;}}}',
'0cae0dd4494d178a04244be03fff68cd', 'Wednesday 12th of November 2008
09:58:29 AM')

I have no error messages but the table column product_data which
ought to hold the serialized data holds only part of the complete
string as shown:

+----------------------+
| product_data |
+----------------------+
| O:4:"cart":4:{s:15:" |
+----------------------+
I understand this might have to do with certain characters within
that serialized data that needs to be escaped. I have attempted a
number of escape options but none seems to work for me. I would
appreciate some assistance with this please. Thanks a lot.

Hi,

I am no mySQL expert, but did you try mysql_real_escape_string() on the
string before using it in the INSERT statement? (I know this works just
as it should in Postgres.)

Also: Could there be a problem with character encoding?
Are both pieces of data in the characterset? (maybe your db uses latin1
and the data passed is Unicode. This can lead to confusing problems.)

Last: DId you try to ask mysql what it says about the length of the
string in that field? Possibly it is all there but your
browser/shell/whatever refuses to display it all because it found funky
charactercontrol functions. I had that situation with a badly adjusted
shell on Linux that was displaying UTF-8. It can often easily be
adjusted in the shell itself.

Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Nov 12 '08 #3
PI
Thanks Erwin and Sven,
Sven; I have tried addslashes and other options on the suggested PHP
man pages - didn't work for this. I am familiar with the addslashes()
function.

Erwin: I have tried the mysql_real_escape_string() function and that
did not work either. On character set and encoding, I use the UTF8 for
the database and the data, how can I be sure/check that the data is
UTF-8. The problem occurs whether the insert is made via PHP or using
a MySQL console.
The other suggestion Erwin made is what I could try, but what command
please would I use to ask for the length of the string value- just in
case neither the console nor the other applications are show the full
string in their display. Thanks a lot.
Nov 12 '08 #4
PI schreef:
Thanks Erwin and Sven,
Sven; I have tried addslashes and other options on the suggested PHP
man pages - didn't work for this. I am familiar with the addslashes()
function.

Erwin: I have tried the mysql_real_escape_string() function and that
did not work either. On character set and encoding, I use the UTF8 for
the database and the data, how can I be sure/check that the data is
UTF-8.
Then you are probably fine, unless MySQL needs extra commands to tell it
to use UTF8 (but I am no good with mysql, so I don't know.)

The problem occurs whether the insert is made via PHP or using
a MySQL console.
The other suggestion Erwin made is what I could try, but what command
please would I use to ask for the length of the string value- just in
case neither the console nor the other applications are show the full
string in their display. Thanks a lot.
A quick google gave me this:
CHAR_LENGTH()
http://dev.mysql.com/doc/refman/5.0/...functions.html

So you'll make a query like:
SELECT CHAR_LENGTH(product_data) as thelength FROM spares_order WHERE
(order_number='tlx-18954XM');

Once again, maybe I make a MySQL mistake, but the above shows the approach.

Good luck.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Nov 12 '08 #5
PI
Thanks again Erwin,
As suggested, I have tried a few commands and tests with the
CHAR_LENGTH command and it appears that the value is indeed stored in
the table but the displays sorts of truncates the values. I hope that
when it is time to retrieve the value and unserialize it, I won't have
the (seemingly)truncated value from the table. However, you've been
brilliant and helpful Erwin, Sven and others. Thanks a huge lot.
Nov 12 '08 #6
PI escribió:
I have some serialized data that I would like to store in a MySQL
database. When viewed, the serialized data looks like this:

O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:18: \"\0cart
\0c_cart_owner\";s:5:\"owner\";s:22:\"\0cart\0c_ca rt_date_time\";N;s:
20:\"\0cart\0c_arr_contents\";a:1:{i:0;O:9:\"cart_ item\":5:{s:
25:\"\0cart_item\0c_cart_item_id\";s:3:\"162\";s:2 7:\"\0cart_item
\0c_cart_item_name\";s:10:\"30BWL15481\";s:34:\"\0 cart_item
\0c_cart_item_description\";s:23:\"BOWL,MINIDOT,PR ISM,POLY\";s:
31:\"\0cart_item\0c_cart_item_quantity\";i:1;s:20: \"\0cart_item
\0c_cart_id\";N;}}}
[...]
When I attempt to insert this data in the table using the following
query statement:

SQL query is is: INSERT INTO spares_order (order_number, user_id,
product_data, session_data, date) VALUES ('tlx-18954XM',
'i**@gmail.com', 'O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:
18:\"\0cart\0c_cart_owner\";s:5:\"owner\";s:22:\"\ 0cart
\0c_cart_date_time\";N;s:20:\"\0cart\0c_arr_conten ts\";a:1:{i:0;O:
9:\"cart_item\":5:{s:25:\"\0cart_item\0c_cart_item _id\";s:3:\"162\";s:
27:\"\0cart_item\0c_cart_item_name\";s:10:\"30BWL1 5481\";s:
34:\"\0cart_item\0c_cart_item_description\";s:
23:\"BOWL,MINIDOT,PRISM,POLY\";s:31:\"\0cart_item\ 0c_cart_item_quantity
\";i:1;s:20:\"\0cart_item\0c_cart_id\";N;}}}',
'0cae0dd4494d178a04244be03fff68cd', 'Wednesday 12th of November 2008
09:58:29 AM')
The backslash is the escape character in MySQL. Your query will not
insert \" but " and it won't insert \0 but a NULL character (i.e., a
byte will all bits set to zero). Once you retrieve the inserted data
your string will containt NULL bytes; some viewers may not display it
properly and stop at the first non-printable char, thus only displaying:
+----------------------+
| product_data |
+----------------------+
| O:4:"cart":4:{s:15:" |
+----------------------+
It also happens that \ is the escape character in PHP, which makes it
all even more confusing.

I presume you don't really know what actual data your variables contain
but it's hard to guess the exact problem without a single line of PHP
code. I've serialized some sample session this way:

print_r( serialize($_SESSION) );

It didn't add any backslash to quotes so you're using a different
serializer, your source data is not a PHP variable or your serialized
data is not exactly what you posted.
I understand this might have to do with certain characters within
that serialized data that needs to be escaped. I have attempted a
number of escape options but none seems to work for me. I would
appreciate some assistance with this please. Thanks a lot.
You don't need to try all the escape options available: each database
access library has its own method. Find out in the manual pages for your
library, whatever it is. PHP's built-in extensions are documented here:

http://www.php.net/manual/en/refs.database.php

Good luck and keep us informed.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Nov 12 '08 #7
PI schreef:
Thanks again Erwin,
As suggested, I have tried a few commands and tests with the
CHAR_LENGTH command and it appears that the value is indeed stored in
the table but the displays sorts of truncates the values. I hope that
when it is time to retrieve the value and unserialize it, I won't have
the (seemingly)truncated value from the table. However, you've been
brilliant and helpful Erwin, Sven and others. Thanks a huge lot.
Wow, us brilliant?
I don't know about Sven, but I am surely not brilliant. ;-)
But thanks a lot. :-) Glad to be of service.

About the truncation: I had the same problem (and a lot more weirder
problems too for that matter). It took me the better part of the day to
find out what I was doing wrong. :-/
It is all about telling your client which characterset to use.
EG: If you are directly on *nix you'll have an evironment variable named
LANG:
type: 'env' in your shell to see your settings. It will give something
like this:
......
LANG=en_US.UTF-8
.....

If not, overrule it via export command.

Or, if you use a program like putty to connect to *nix from Windows,
like me, you can set it too for every saved connection.
You can find it under Window->Translation:
'Received data assumed to be in which character set'
Select your choice from the dropdownlist.

Good luck.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Nov 12 '08 #8
PI wrote:
Thanks Erwin and Sven,
Sven; I have tried addslashes and other options on the suggested PHP
man pages - didn't work for this. I am familiar with the addslashes()
function.

Erwin: I have tried the mysql_real_escape_string() function and that
did not work either. On character set and encoding, I use the UTF8 for
the database and the data, how can I be sure/check that the data is
UTF-8. The problem occurs whether the insert is made via PHP or using
a MySQL console.
The other suggestion Erwin made is what I could try, but what command
please would I use to ask for the length of the string value- just in
case neither the console nor the other applications are show the full
string in their display. Thanks a lot.
mysql_real_escape_string() is the correct function to use - you should
not be using addslashes().

How are you looking at your data? If you're using phpMyAdmin, it
truncates long columns by default.

If you're still having problems, you should be asking this in
comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 12 '08 #9
On 11ÔÂ12ÈÕ, ÏÂÎç6ʱ49·Ö, PI <ire.oguns...@gmail.comwrote:
Hi Guys,

I need some assistance with the following scenario please. I know this
might be more of a MySQL post than a PHP one, but it is likely some
developer has been here before and would be able to help out.

I have some serialized data that I would like to store in a MySQL
database. When viewed, the serialized data looks like this:

O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:18: \"\0cart
\0c_cart_owner\";s:5:\"owner\";s:22:\"\0cart\0c_ca rt_date_time\";N;s:
20:\"\0cart\0c_arr_contents\";a:1:{i:0;O:9:\"cart_ item\":5:{s:
25:\"\0cart_item\0c_cart_item_id\";s:3:\"162\";s:2 7:\"\0cart_item
\0c_cart_item_name\";s:10:\"30BWL15481\";s:34:\"\0 cart_item
\0c_cart_item_description\";s:23:\"BOWL,MINIDOT,PR ISM,POLY\";s:
31:\"\0cart_item\0c_cart_item_quantity\";i:1;s:20: \"\0cart_item
\0c_cart_id\";N;}}}

The problem is that I have not been able to successfully persist this
data in the database. The Schema I am currently using for the table
is:
+--------------+--------------+------+-----+---------+----------------
+
| Field | Type | Null | Key | Default | Extra
|
+--------------+--------------+------+-----+---------+----------------
+
| order_id | int(10) | NO | PRI | NULL | auto_increment
|
| order_number | varchar(255) | YES | | NULL |
|
| user_id | varchar(255) | YES | | NULL |
|
| product_data | longtext | YES | | NULL |
|
| session_data | varchar(255) | YES | | NULL |
|
| date | varchar(255) | YES | | NULL |
|
+--------------+--------------+

When I attempt to insert this data in the table using the following
query statement:

SQL query is is: INSERT INTO spares_order (order_number, user_id,
product_data, session_data, date) VALUES ('tlx-18954XM',
'...@gmail.com', 'O:4:\"cart\":4:{s:15:\"\0cart\0c_cart_id\";N;s:
18:\"\0cart\0c_cart_owner\";s:5:\"owner\";s:22:\"\ 0cart
\0c_cart_date_time\";N;s:20:\"\0cart\0c_arr_conten ts\";a:1:{i:0;O:
9:\"cart_item\":5:{s:25:\"\0cart_item\0c_cart_item _id\";s:3:\"162\";s:
27:\"\0cart_item\0c_cart_item_name\";s:10:\"30BWL1 5481\";s:
34:\"\0cart_item\0c_cart_item_description\";s:
23:\"BOWL,MINIDOT,PRISM,POLY\";s:31:\"\0cart_item\ 0c_cart_item_quantity
\";i:1;s:20:\"\0cart_item\0c_cart_id\";N;}}}',
'0cae0dd4494d178a04244be03fff68cd', 'Wednesday 12th of November 2008
09:58:29 AM')

I have no error messages but the table column product_data which
ought to hold the serialized data holds only part of the complete
string as shown:

+----------------------+
| product_data |
+----------------------+
| O:4:"cart":4:{s:15:" |
+----------------------+

I understand this might have to do with certain characters within
that serialized data that needs to be escaped. I have attempted a
number of escape options but none seems to work for me. I would
appreciate some assistance with this please. Thanks a lot.
do
mysql_real_escape_string()
mysqli_real_escape_string()

before doing insert/update/replace to mysql
Nov 13 '08 #10

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

Similar topics

3
3155
by: Alexander Ross | last post by:
I know your first reaction will be "WHY?!?!" But can it be done? Can I store a php array in a postgresql database? -- Alexander Ross alexross@bleen.net
8
2161
by: Steven | last post by:
Hi there, I am wanting to store price data as n.nn format, so if the user enters "1" the data that gets stored is "1.00" Is there a way to do this. Cheers Steven
3
11737
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
0
1642
by: Eben Goodman | last post by:
I am storing book isbn numbers in a table. isbn numbers are 10 digit numbers and many start with 0. The data type of the field I am storing this info in is a bigint(16) unsigned. It appears that...
24
2105
by: Ehud Shabtai | last post by:
Hi, I would like to store samples of data which are gathered from about 500 devices in mysql. Each device has its own data which changes over time. The data may be integer or float. The...
3
4701
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
10
3054
by: Diego F. | last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is the easiest way to do it? Do I need to write methods to store each attribute separately from C# app to the table and the...
2
1393
by: hendry.johan | last post by:
Hi, I'm currently developing an HR system which involves storing a lot of configurations per module, such as payroll module, absence/shift module, training module, etc. total: around 100...
6
3160
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.