2016-12-06 2 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/의 옵션 부분을 참조하십시오. manual/ko/function.json-encode.php – Fky

+0

나는 HTML 엔티티를 구현했으며 필드를 null에서 empty로 변경했습니다. 나는 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

관련 문제