2011-11-25 47 views
1

元音變音我有一個配方表:錯誤的編碼與在PHP

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」:「劑量 \ u00f6ffnen」,「COUNT_PERSONS」 「4」,「DURATION」:「10」,「USER_ID」:「1」,「DATE」:「2011-12-31 23:59:59」}}},「Message」:null, :200}

我得到'ö'作爲char代碼。

如何解決這個問題?

感謝

+2

你的問題是什麼? '\ u00f6'是有效的json。 – hakre

回答

8

我得到的 'O' 爲char代碼。如何解決這個問題呢?

你不知道。 \u00f6是JSON中完全合法的字符轉義碼。一旦JSON被解析,它將被解析爲ö