PHP Json_encode after gzdeflate then insert MySQL database, insert failed

Source: Internet
Author: User

$a = array (' AA ' = ' BB ');
$s = json_encode ($a);
$s = gzdeflate ($s);//I missed the sentence when I raised the question, and it was a big mistake (2014/12/22 18:20:00)
$sql = "INSERT into Tbl_name (id,content) VALUES (1, '%s ')";
$sql = sprintf ($sql, $s);
Mysql_connect (' Host ', ' User ', ' pwd ');
mysql_query (' Set names UTF8 ');
mysql_query (' use db_name ');
mysql_query ($sql);
Mysql_close ();

The above code executes after the database has a new record, but the value of the Content field is empty, please show the hero, my question where?

Add
I printed sql: INSERT into tbl_name (id,content) VALUES (1, ' klü0\0 ')
The results database contains the following content:

Feel like the character set is not supported, have checked the database and table are UTF8?

And then, what are some of the things I should be aware of when I'm fetching data?

Reply content:

$a = array (' AA ' = ' BB ');
$s = json_encode ($a);
$s = gzdeflate ($s);//I missed the sentence when I raised the question, and it was a big mistake (2014/12/22 18:20:00)
$sql = "INSERT into Tbl_name (id,content) VALUES (1, '%s ')";
$sql = sprintf ($sql, $s);
Mysql_connect (' Host ', ' User ', ' pwd ');
mysql_query (' Set names UTF8 ');
mysql_query (' use db_name ');
mysql_query ($sql);
Mysql_close ();

The above code executes after the database has a new record, but the value of the Content field is empty, please show the hero, my question where?

Add
I printed sql: INSERT into tbl_name (id,content) VALUES (1, ' klü0\0 ')
The results database contains the following content:

Feel like the character set is not supported, have checked the database and table are UTF8?

And then, what are some of the things I should be aware of when I'm fetching data?

That's not 字符集 the problem, it 编码 's the problem.
gzdeflateAfter the data is binary string, does not belong to any 字符集 , the character set is targeted 可打印(printable)文本串

You're utf8_encode actually converting the gzdeflate after the 二进制串 encoding to encode iso_8859_1 字符串 utf-8 , so one step will only increase the length of the compressed binary string (all ascii>127 characters will be expanded to 2 bytes or more)

If the compression is considered, it is best to use BLOB the same binary type storage, the access time does not need to encode the conversion

Problem solved, is indeed a character set problem, the Gzdeflate compressed data to utf8_encode a bit on it.

$a = array (' AA ' = ' BB ');
$s = json_encode ($a);
$s = gzdeflate ($s);
$s = utf8_encode ($s);//The answer is here, take out the time corresponding to do Utf_decode ()
$sql = "INSERT into Tbl_name (id,content) VALUES (1, '%s ')";
$sql = sprintf ($sql, $s);
Mysql_connect (' Host ', ' User ', ' pwd ');
mysql_query (' Set names UTF8 ');
mysql_query (' use db_name ');
mysql_query ($sql);
Mysql_close ();

Refer to the information here http://stackoverflow.com/questions/9413402/php-mysql-special-character-inserts-being-truncated

Compared to the size of the front of the gzdeflate data, the original 280,000 bytes of content compression after only 2000, compression ratio is quite high

Yes, you can. Gzdeflate data is then base64 to enhance compatibility, or the binary stream after deflate is stored directly in the database

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.