简体   繁体   中英

Wrong Encoding with umlauts in PHP

I have a Recipe Table:

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 ;

A insert:

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

With php i parse it to a 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);

?>

My Problem is when i call getRecipeByID.php?=20:

{"Data":{"Recipes":{"Recipe_20":{"ID":"20","TITLE":"Hundefutter","TEXT":"Dose \öffnen","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?

Thanks

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

You don't. is a perfectly legal character escape code in JSON. Once the JSON gets parsed, it will be resolved as an ö .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM