Problem:
When using Ajax to transfer data to the server in get mode on Google Map, the server side displays the url as garbled characters.
Zend Framework stores data in MySQL as garbled characters, but when extracted, it is in normal Chinese fonts.
When Chinese characters are entered into MySQL, they are displayed as garbled characters on the PHP web page.
Solution:
1. Ajax url encoding needs to be converted. I use the following function:
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 places involving encoding to utf8(php) or utf-8
MySQL includes database, data table, fields, and database connection.
Zend Framework includes data connections, views and html output.
Ajax includes Javascript, XML file encoding and font encoding.
Zend Framework data connection encoding settings:
Refer to http://phpeye.com/bbs/redirect.php?fid=2&tid=81&goto=nextoldset
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter', $dbAdapter);
$dbAdapter->query("SET NAMES 'utf8'");
If it is a direct PHP connection, set it like this:
// 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'");