Fixed the issue of garbled characters in Ajax, MySQL, and Zend framework.
Source: Internet
Author: User
Problem:
When Google map uses ajax to transmit data to the server in get mode, the server displays garbled URLs,
Zend framework stores garbled data to MySQL, but the extracted data is a normal Chinese font,
Enter Chinese Characters in MySQL and garbled characters are displayed on the PHP webpage.
Solution:
1. Ajax URL encoding needs to be converted. The following functions are used:
Public Function js_unescape ($ Str)
{
$ Ret = '';
$ Len = strlen ($ Str );
For ($ I = 0; $ I <$ Len; $ I ++)
{
If ($ STR [$ I] = '%' & $ STR [$ I + 1] = 'U ')
{
$ Val = hexdec (substr ($ STR, $ I + 2, 4 ));
If ($ Val <0x7f) $ ret. = CHR ($ Val );
Else if ($ Val <0x800) $ ret. = CHR (0xc0 | ($ Val> 6). CHR (0x80 | ($ Val & 0x3f ));
Else $ ret. = CHR (0xe0 | ($ Val> 12 )). CHR (0x80 | ($ Val> 6) & 0x3f )). CHR (0x80 | ($ Val & 0x3f ));
$ I + = 5;
}
Else if ($ STR [$ I] = '% ')
{
$ Ret. = urldecode (substr ($ STR, $ I, 3 ));
$ I + = 2;
}
Else $ ret. = $ STR [$ I];
}
Return $ ret;
}
Call example: $ row-> name = xmlcontroller: js_unescape ($ this-> getrequest ()-> getparam ('name '));
2. Set all encoding-related fields to utf8 (PHP) or UTF-8.
MySQL includes databases, data tables, fields, and database connections.
Zend framework includes data connections, views, and HTML output.
Ajax includes JavaScript and XML file encoding and font encoding.
Zend framework data connection encoding settings:
Reference http://phpeye.com/bbs/redirect.php? FID = 2 & tid = 81 & goto = nextoldset
Zend_db_table: setdefaadapter adapter ($ dbadapter );
Zend_registry: Set ('dbadapter ', $ dbadapter );
$ Dbadapter-> query ("set names 'utf8 '");
If it is a PHP direct connection, the settings are as follows:
// Select all the rows in the markers table
$ Query = "select * from markers where 1 ";
// Add this sentence after the select database
Mysql_query ("set names utf8 ");
Mysql_query ("set Character Set utf8 ");
Mysql_query ("set collation_connection = 'utf8 _ general_ci '");
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