Home > Backend Development > PHP Tutorial > Solved the garbled problem of Ajax, MySQL and Zend Framework_PHP Tutorial

Solved the garbled problem of Ajax, MySQL and Zend Framework_PHP Tutorial

WBOY
Release: 2016-07-21 15:47:10
Original
1061 people have browsed it

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'");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319956.htmlTechArticleProblem: When using Ajax to transfer data to the server in get mode on Google Map, the server side displays garbled URL. Zend Framework stores data in MySQL as garbled characters, but when extracted...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template