Home > php教程 > php手册 > body text

解决了Ajax、MySQL 和 Zend Framework 的乱码问题

WBOY
Release: 2016-06-13 12:24:33
Original
818 people have browsed it

问题:
在 Google Map 上用 Ajax 以 get 方式向服务器传递数据时,服务器端显示为 url 乱码,
Zend Framework 向 MySQL 存储数据为乱码,但是提取出来又是正常中文字体,
MySQL 中输入中文,显示在 PHP 网页上为乱码。
解决方式:
1. Ajax 的 url 编码需要转换,我使用的如下函数:
public function js_unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i {
if ($str[$i] == '%' && $str[$i+1] == 'u')
{
$val = hexdec(substr($str, $i+2, 4));
if ($val else if($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;
}
调用示例: $row->name =XmlController::js_unescape( $this->getRequest()->getParam('name') );
2. 把所有涉及编码的地方都设为 utf8(php) 或 utf-8
MySQL 包括数据库,数据表,字段,数据库连接。
Zend Framework 包括数据连接,视图和html输出。
Ajax 包括 Javascript ,XML 的文件编码和字体编码。
Zend Framework数据连接编码设置:
参考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'");
如果是 PHP 直接连接则这样设置:
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
//select 数据库之后加多这一句
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!