472,984 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 5447
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
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
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
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
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
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
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
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.