5

I have been trying to compress and store a json encoded string into mysql, but I am getting "unexpected /" errors.

I also tried to use addslashes like this:

addslashes(gzcompress(json_encode($mystring)));

And to display

json_decode(gzuncompress(stripslashes($mystring)));

But it fails on insert with the error I mentioned.

I read somewhere a string with gzcompress should be stored as a blob, but I was hoping there is a way to store it in a mysql text field so I dont have to mess with the db.

PS: Some asked for full error message here it is:

Warning: Unexpected character in input: '\' (ASCII=92) state=1

PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x9C\xED}\x8Br\xDB...' for column 'field_text_value' at row 1.

11
  • mysql_real_escape_string should be used instead of addslashes (not saying that will fix this problem; I mean in general). addslashes is not aware of character sets, whereas mysql_real_escape_string is actually aware of the database connection and can more accurately escape data. addslashes() is one of the many things PHP did trying to help coders that should have never been done. Commented Dec 15, 2011 at 7:02
  • Thanks Corbin, will check that out as well. By the way whoever minus oned this please give an explanation why. Commented Dec 15, 2011 at 7:02
  • Storing "big arrays" in a relational database makes very little sense. Asking questions providing them with not a full error message but rather with a stub says '"unexpected /"' makes even less sense. It is even impossible to tell, what part of the code issues this stub Commented Dec 15, 2011 at 7:03
  • @Corbin mysql_real_escape_string is not aware as well, unless you explicitly tell it otherwise. thus, just a recommendation "use it, not addslashes" will actually make no change at all. Commented Dec 15, 2011 at 7:05
  • Are you sure about that? "Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query()." And as for the optional connection parameter: " If the link identifier is not specified, the last link opened by mysql_connect() is assumed." Commented Dec 15, 2011 at 7:07

2 Answers 2

11

Store it as a BLOB. Even if there were a way to store it in a VARCHAR or *TEXT field in a way that survives a round trip, it would be a horrible way.

Are you sure you need compression anyway?

You can also make MYSQL do the compression, e.g. INSERT INTO mytable (compressed_json) VALUE (COMPRESS('[\"the json\"]').

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I am not sure, but gzcompress looked like a nice way to compress a string. Will investigate the blob way.
gzcompress produces binary data. BLOB column data type stores binary data. If you're not using gzcompress there's no point using BLOB. (All "blob" says is "mysql, do not try to rewrite these bits. This is not text, it has no encoding". How you put your bits in, whether with gzcompress or COMPRESS(), is up to you.)
Thanks it works with BLOB. Much appreciated. It takes up much less space with gzcompress.
Final numbers are out, managed to compress a table with 30 000 records, 1.7 GB, big json encoded arrays to 300 MB :D WOW Awesome. Added a new blob field, imported text field and gzcompressed it. Then deleted the old field.
-2

Why would you want to add a gzip compressed string into a database? Why don't you just store it as:

mysql_real_escape_string(json_encode($myArray)))

1 Comment

It's a big array, I was hoping compression would make the arrays json encoded string even smaller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.