在php中编码罗马尼亚字符

时间:2013-08-08 10:21:38

标签: php mysql character-encoding

我有编码字符的问题:ĂăÂâÎŢţ 我正在使用以下mysql表:

CREATE TABLE `news` (
  `NewsID` int(11) NOT NULL AUTO_INCREMENT,
  `UserID` int(11) NOT NULL,
  `Title` varchar(255) CHARACTER SET utf8 NOT NULL,
  `Date` datetime NOT NULL,
  PRIMARY KEY (`NewsID`),
  FULLTEXT KEY `Title` (`Title`,`Content`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

我尝试使用以下代码(在zend框架上运行)在Title字段中插入上面提到的字符序列:

$params = $this->getRequest()->getParams(); 
$mysqli = new mysqli("localhost", "user", "pass", "database_name");
$mysqli->query("INSERT INTO `news` (`NewsID`, `Title`) VALUES (NULL, '".$params['text']."');");

在数据库中,我得到字段Title以下值:ÃãÂâÎîȘșȚț 为什么这些字符是html编码的?为什么不是第一个编码为utf8_bin等效字符的字符? 感谢。

3 个答案:

答案 0 :(得分:2)

对于我来说,我只是用以下行更新了php db连接设置:

--diff-filter=d

我还以@liyakat的名义添加了HTML文件mysqli_set_charset( $con, 'utf8');

旧线程,但也许有人需要知道这一点。

答案 1 :(得分:0)

确保您的IDE或文本编辑器也设置为使用UTF-8字符。

答案 2 :(得分:0)

要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf

[客户] 默认字符集= UTF8

的[mysqld] default-character-set = utf8 然后,验证:

mysql> show variables like "%character%";show variables like "%collation%";

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

或尝试

尝试将MySQL连接设置为UTF-8:

SET NAMES 'utf8'

并发送明确的UTF-8标头,以防您的服务器具有其他一些默认设置:

header('Content-type: text/html; charset=utf-8');