php / mysql:如何在文本mysql字段中插入gzcompress-ed字符串? [英] php/mysql : How to insert a gzcompress-ed string into a text mysql field?
问题描述
我一直试图压缩和存储一个json编码字符串到mysql,但我得到意外/错误。
I have been trying to compress and store a json encoded string into mysql, but I am getting "unexpected /" errors.
我也试图使用addslashes this:
I also tried to use addslashes like this:
addslashes(gzcompress(json_encode($mystring)));
并显示
json_decode(gzuncompress(stripslashes($mystring)));
但是在插入时失败,出现了我提到的错误。
But it fails on insert with the error I mentioned.
我读了一个字符串与gzcompress应该存储为一个blob,但我希望有一种方式将其存储在一个mysql文本字段,所以我不必打乱数据库。
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:有些人要求输入完整的错误讯息:
PS: Some asked for full error message here it is:
警告: :'\'(ASCII = 92)state = 1
Warning: Unexpected character in input: '\' (ASCII=92) state=1
PDOException:SQLSTATE [HY000]:一般错误:1366不正确的字符串
value:'\
PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x9C\xED}\x8Br\xDB...' for column 'field_text_value' at row 1.
推荐答案
将它存储为 BLOB
。即使有一种方法可以存储在 VARCHAR
或 * TEXT
字段中,这将是一个可怕的方式。
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.
您确定需要压缩吗?
MYSQL做压缩,例如 INSERT INTO mytable(compressed_json)VALUE(COMPRESS('[\the json \]')
。
You can also make MYSQL do the compression, e.g. INSERT INTO mytable (compressed_json) VALUE (COMPRESS('[\"the json\"]')
.
这篇关于php / mysql:如何在文本mysql字段中插入gzcompress-ed字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!