Home > Article > Backend Development > zend framework solves the garbled problem of Ajax, MySQL and Zend Framework
Problem:
When using Ajax to transmit 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 normal Chinese font.
Input Chinese into MySQL. The code displayed on the PHP web page is garbled.
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;
}
Calling example: $row->name =XmlController::js_unescape( $this->getRequest()->getParam('name') );
2. Set all encoding areas 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:
Reference 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'");
The above introduces the zend framework to solve the garbled problem of Ajax, MySQL and Zend Framework, including the content of zend framework. I hope it will be helpful to friends who are interested in PHP tutorials.