2016-12-06 6 views
0

HTMLタグを含むテーブルに一部のデータを公開しようとしています。ページにアクセスすると、HTMLを含まないフィールドはすべて正常ですが、HTMLを含むフィールドはすべてnullです。htmlタグを含むときにnullを返すJSONフィールド

フィールドをVarCharとTextに設定しようとしましたが、どちらもうまくいかないようです。テーブルはutf8_general_ciにも設定されています。

request_step_content.php

<?php 
header("Content-Type:application/json"); 
header("Access-Control-Allow-Origin: *"); 

$user = "userName"; 
$pass = "userPassword"; 
$table = "myTable"; 

$db=new PDO("mysql:host=localhost;dbname=$table", $user, $pass); 

$query = "SELECT * FROM steps"; 

try { 
    $stmt = $db->query($query); 
    } 
catch (PDOException $ex) { 
    $response["success"] = 0; 
    $response["message"] = "Database Error."; 
    die(json_encode($response)); 
} 

$rows = $stmt->fetchAll(); 

if($rows) { 
    $response["success"] = 1; 
    $response["message"] = "Step"; 
    $response["step"] = array(); 

    foreach ($rows as $row) { 
     $post = array(); 
     $post["id"] = $row["intID"]; 
     $post["heading"] = $row["strStepheading"]; 
     $post["keyPrinciple"] = $row["strKeyPrinciple"]; 
     $post["pillar"] = $row["strPillar"]; 
     $post["introduction"] = $row["memIntroduction"]; 
     $post["actionSteps"] = $row["memActionSteps"]; 
     $post["actionPoints"] = $row["memActionPoints"]; 
     $post["studyAndUnderstanding"] = $row["memStudyUnderstanding"]; 

     array_push($response["step"], $post); 
    } 
    echo json_encode($response); 
} 
else { 
    $response["success"] = 0; 
    $response["message"] = "No Steps Available"; 
    die(json_encode($response)); 
} 
?> 

JSONレスポンス

{"success":1, 
"message": 
      "Step", 
      "step":[{"id":"1", 
          "heading":"Test Heading", 
          "keyPrinciple":"Key Principle: Test Key Principle", 
          "pillar":"Pillar 1: Pillar", 
          "introduction":null, 
          "actionSteps":null, 
          "actionPoints":null, 
          "studyAndUnderstanding":null}]} 
+0

htmlentities関数を使用するhtmlを含む$ rowにhttp://php.net/manual/fr/function.htmlentities.phpを使用すると、json_encodeのオプションを使用して完了できます。詳細については、http://php.net/を参照してください。マニュアル/ ja/function.json-encode.php – Fky

+0

私はhtmlエンティティを実装し、フィールドをnullから空に変更しました。私はjson_encodeオプションを使って何をすべきか100%ではないのですか? – geolaw

答えて

0

エンコードに関する@Fkyの答えの助けを借りて問題を修正しました。

使用するutf_encode()私のJSONフィールドには、htmlコンテンツを含むすべてのフィールドがあります。編集コードは次のとおりです。

 foreach ($rows as $row) { 
     $post = array(); 
     $post["id"] = $row["intID"]; 
     $post["heading"] = $row["strStepheading"]; 
     $post["keyPrinciple"] = $row["strKeyPrinciple"]; 
     $post["pillar"] = $row["strPillar"]; 
     $post["introduction"] = utf8_encode($row["memIntroduction"]); 
     $post["actionSteps"] = utf8_encode($row["memActionSteps"]); 
     $post["actionPoints"] = utf8_encode($row["memActionPoints"]); 
     $post["studyAndUnderstanding"] = utf8_encode($row["memStudyUnderstanding"]); 

ありがとうございました。

+0

お役立ち情報 – Fky

0

問題は、エンコーディングであろう。 DBテーブルの列はUTF-8でもかまいませんが、格納されたデータはUTF-8でなければなりません。 PHP関数json_encodeはUTF-8データのみを受け入れます。保存されているDBデータを確認してください。

+0

ありがとう!答えに私を置き、 'utf_encode($ string)'に配列要素をラップしてみましょう。 – geolaw

+0

これを返信としてマークしてください。ありがとう。 – sajushko

関連する問題