php - PHP 中的元音编码错误

标签 php mysql json character-encoding

我有一个食谱表:

CREATE TABLE IF NOT EXISTS `RECIPES` (
  `recipes_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `text` varchar(2000) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `count_persons` int(11) NOT NULL,
  `duration` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `accepted` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`recipes_id`),
  KEY `recipes_user_fk` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=44 ;

插入:

(20, 'Hundefutter', 'Dose öffnen', 4, 10, 1, '2011-12-31 23:59:5', 0)

我用 php 将它解析为 json (getByID):

<?php
header('Content-Type: text/html; charset=utf8');
$id =  $_GET['id'];
include 'db_connect.php';
$arr = array('Data' => null,'Message' => null,'Code' => null);
        if (is_numeric($id))
        {
            //include 'db_connect.php';
            $id =  mysql_real_escape_string($_GET['id']);
            mysql_query("SET NAMES 'utf8'");
            mysql_query("SET CHARACTER SET 'utf8'");
            $sql = "Select * from RECIPES WHERE recipes_id=".$id;
            $result = mysql_query($sql,$db) or exit("QUERY FAILED!");

            while($row = mysql_fetch_array($result))
             {
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['ID'] = $row['recipes_id'];
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['TITLE'] = $row['title'];
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['TEXT'] = utf8_decode($row['text']);
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['COUNT_PERSONS'] = $row['count_persons'];
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['DURATION'] = $row['duration'];
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['USER_ID'] = $row['user_id'];
                 $arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['DATE'] = $row['date'];
             }
             if(count($arr['Data']['Recipes'])==1)
             {
                $arr['Code'] = 200;

             }
             else
             {
                $arr['Code'] = 404;
                $arr['Message'] = "not found";
             }

        }


        else
        {
                $arr['Code'] = 400;
                $arr['Message'] = "Bad Request";
        }
        mysql_close($db);
        echo json_encode($arr);

?>

我的问题是当我调用 getRecipeByID.php?=20:

{"Data":{"Recipes":{"Recipe_20":{"ID":"20","TITLE":"Hundefutter","TEXT":"Dose \u00f6ffnen","COUNT_PERSONS":"4","DURATION":"10","USER_ID":"1","DATE":"2011-12-31 23:59:59"}}},"Message":null,"Code":200}

我得到 'ö' 作为字符代码。

如何解决这个问题?

谢谢

最佳答案

I get the 'ö' as char code. How to solve this problem?

你不知道。 \u00f6 是 JSON 中完全合法的字符转义码。解析 JSON 后,它将解析为 ö

关于php - PHP 中的元音编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8266465/

相关文章:

python - 停止 python 将 json 文件写入一行

jquery - 如何在具有现有记录的 json 数据表顶部添加新行

javascript - 使用PHP将JSON插入MySQL表

php - 查询性能调试

php - 使用 Docker 运行 Laravel artisan 命令时连接被拒绝

php - 使用php循环遍历mysql表,为什么elseif语句不起作用?

mysql - 选择指定列中具有最高值的行,并且其他列中不具有唯一值的行应该是不同的

php - 我在哪里可以找到关于 JSON 和 PHP 的好视频或教程?

mysql - Laravel 将动物与多个标签相关联

mysql - 与 App Engine 共享的计算引擎 SQL