Solve the Ajax, MySQL and Zend Framework of garbled problems _php skills
Source: Internet
Author: User
Problem:
When using Ajax to pass data to the server on Google Map, the server side is displayed as URL garbled,
The Zend Framework stores data to MySQL as garbled, but extracts it as a normal Chinese font,
MySQL input Chinese, displayed on the PHP page for garbled.
Solution Method:
1. Ajax URL encoding needs to be converted, and I use the following functions:
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. UTF8 (PHP) or utf-8 where all the coding is involved
MySQL includes databases, datasheets, fields, database connections.
The 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 this is a direct connection to PHP, set this up:
Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
After the Select database Cadogan this sentence
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