0

I'm trying to display a json array with contents from my database but when their is accents in the database the json array doesn't want to be displayed, their is my code:

<?php

include 'connect.php';

header('Content-Type: text/plain; charset=UTF-8') ;

$serial = 1;

$statment = mysqli_prepare($conn, "SELECT * FROM lesson WHERE serial = ?");
mysqli_stmt_bind_param($statment, "i", $serial);
mysqli_stmt_execute($statment);

mysqli_stmt_store_result($statment);
mysqli_stmt_bind_result($statment, $serial, $by, $type, $description, $lesson, $nb_q);

$response = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statment)){
    $response["success"] = true;
    $response["serial"] = $serial;
    $response["by"] = $by;
    $response["type"] = $type;
    $response["lesson"] = $lesson;
    $response["description"] = $description;
    $response["nb_q"] = $nb_q;
    for ($i = 1; $i <= $nb_q; $i++) {

        $statment2 = mysqli_prepare($conn, "SELECT * FROM question WHERE 4_l_id = ? AND q_num = ?");
        mysqli_stmt_bind_param($statment2, "ii", $serial, $i);
        mysqli_stmt_execute($statment2);

        mysqli_stmt_store_result($statment2);
        mysqli_stmt_bind_result($statment2, $question, $nb_answer, $type, $correct_1, $for_l_id, $id, $q_num);
        while(mysqli_stmt_fetch($statment2)){
            $response["q".$i] = $question;
        }
    }
}

echo json_encode($response, JSON_UNESCAPED_UNICODE);

?>

Thank you for your answers

1 Answers1

0

I found a good code but thank you for your help,

<?php

include 'connect.php';


$serial = 1;

$statment = mysqli_prepare($conn, "SELECT * FROM lesson WHERE serial = ?");
mysqli_stmt_bind_param($statment, "i", $serial);
mysqli_stmt_execute($statment);

mysqli_stmt_store_result($statment);
mysqli_stmt_bind_result($statment, $serial, $by, $type, $description, $lesson, $nb_q);

$response = array();
$response["success"] = false;

function utf8_encode_all(&$value)
{
  if (is_string($value)) return utf8_encode($value);
  if (!is_array($value)) return $value;
  $ret = array();
  foreach($dat as $i=>$d) $ret[$i] = utf8_encode_all($d);
  return $ret;
}

while(mysqli_stmt_fetch($statment)){
    $response["success"] = true;
    $response["serial"] = $serial;
    $response["by"] = utf8_encode_all($by);
    $response["type"] = utf8_encode_all($type);
    $response["lesson"] = utf8_encode_all($lesson);
    $response["description"] = utf8_encode_all($description);
    $response["nb_q"] = $nb_q;
    for ($i = 1; $i <= $nb_q; $i++) {

        $statment2 = mysqli_prepare($conn, "SELECT * FROM question WHERE 4_l_id = ? AND q_num = ?");
        mysqli_stmt_bind_param($statment2, "ii", $serial, $i);
        mysqli_stmt_execute($statment2);

        mysqli_stmt_store_result($statment2);
        mysqli_stmt_bind_result($statment2, $question, $nb_answer, $type, $correct_1, $for_l_id, $id, $q_num);
        while(mysqli_stmt_fetch($statment2)){
            $response["q".$i] = utf8_encode_all($question);
        }
    }
}

$array = array_map('htmlentities',$response);

$json = html_entity_decode(json_encode($array));

echo $json;

?>