Mysq, php writes unicode characters

Source: Internet
Author: User
: This article describes how to write unicode characters in mysq and php. For more information about PHP tutorials, see.
Some special characters (icon characters) cannot be inserted into the database when saving mysql. you can convert the characters (special characters and normal characters) to base64 encoding using base64_encode. when saving the characters to mysql for retrieval and restoration, use base64_decode for base64 decoding, and then use json_decode to restore to the original character

Sort by reference to online materials

/*** Some special characters (icon characters) cannot be inserted into the database when saving mysql * you can convert the characters (special characters and normal characters) to base64 encoding using base64_encode, save it to mysql * and perform base64 decoding using base64_decode, use json_decode to restore to the original character * // *** unicode decoding * @ param $ str * @ return mixed | string */function unicode_decode ($ str) {if (! $ Str) return $ str; $ decode = json_decode ($ str); if ($ decode) return $ decode; $ str = '["'. $ str. '"]'; $ decode = json_decode ($ str); if (count ($ decode) = 1) {return $ decode [0];} return $ str ;} // $ re = unicode_decode ('\ u963f \ u55b5 \ ud83d \ udc31 \ ud83d \ udca6'); // echo ($ re ); /*** unicode encoding ** @ param $ str * @ return string */function unicode_encode ($ str) {if (! $ Str) return $ str; $ decode = json_encode ($ str); if ($ decode) return $ decode; $ str = '["'. $ str. '"]'; $ decode = json_encode ($ str); if (count ($ decode) = 1) {return $ decode [0];} return $ str ;} // $ re1 = unicode_encode ('amount'); // echo ($ re1 ); //////////////////////////////////////// /// // *** encode the unicode character in Base64, save to database */$ conn = @ mysql_connect ("localhost", "root", "111111"); if (! $ Conn) {die ("failed to connect to the database :". mysql_error ();} mysql_select_db ("wx_sys", $ conn); mysql_query ("set character set 'gbk '"); // avoid converting Chinese garbled characters to mysql_query ("set character set 'utf8 '"); // use the PHP file in UTF-8 format // mysql_query ("set names 'utf8 '"); // when the PHP file is in UTF-8 format, use the // unicode code character $ name = '\ u963f \ u55b5 \ ud83d \ udc31 \ ud83d \ udca6 '; // normal Chinese // $ name = 'Zhang San'; // base64 encoded $ re = base64_encode ($ name); $ SQL = "insert into test (uni) values (' ". $ Re." ') "; if (! Mysql_query ($ SQL, $ conn) {echo "failed to add data:". mysql_error ();} else {echo "data added successfully! ";} // *** Retrieves the data and decodes it by base64, and restore the character */$ sql2 = "select * from test"; $ result = mysql_query ($ sql2) or die ("invalid query :". mysql_error (); // cyclically retrieve data from the dataset while ($ row = mysql_fetch_array ($ result) {$ sr = base64_decode ($ row ['uni']); $ AB = unicode_decode ($ sr); echo "uni :". $ AB."
";}

The above section describes how to write unicode characters in mysq and php, including some content. I hope to help anyone who is interested in PHP tutorials.

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.