Special characters returning in Json in PHP

Asked

Viewed 44 times

-1

I made an "api" to return the data in json format, however the record that contains accent, is displaying special characters.

Look at my code, what I could do, there’s some command I can add?

I tried to put some parameters in json_encode, but it didn’t work.

Please I really need this help! I’ve done a lot of research before coming here.

My server php is already in version 7

      <?php
    
    header('Content-type: application/json');

   require_once('dbConnect.php');

    mysqli_set_charset($conn, $charset);

    $response = array();

    $stmt = mysqli_prepare($conn, "SELECT codigo, estado FROM estados");

   mysqli_stmt_execute($stmt);
   
   mysqli_stmt_store_result($stmt);

   mysqli_stmt_bind_result($stmt, $codigo, $estado);
 
 if (mysqli_stmt_num_rows($stmt) > 0) {
   while (mysqli_stmt_fetch($stmt)) {
     array_push($response, array("codigo" => $codigo,"estado" => $estado));
   }

    echo json_encode($response);
    } else {

       echo json_encode($response);

    }
?>

The data is like this:

inserir a descrição da imagem aqui

1 answer

-2

I got a solution for anyone with the same problem. I used this:

$sql = mysqli_query($conn,"SELECT codigo, estado FROM estados");
$linhas=mysqli_num_rows($sql); 


if( $linhas > 0 ) {
    while($resultado = mysqli_fetch_assoc($sql)){
      //  $vetor[]= array_map('utf8_encode', $resultado); 
        $vetor[] =$resultado;
}    
    //Passando vetor em forma de json
    
    echo   json_encode($vetor);
} 
else
{
    
    $array = array(
        "codigo" => "0",
        "estado" => "vazio"
    );

    $vetor[] = array_map('utf8_encode', $array); 

    echo   json_encode($vetor);

 }
  • Imagine if you have 1 million results and you have to use array_map, plus Pog which solution hehe. I recommend the post https://answall.com/q/43193/4800

Browser other questions tagged

You are not signed in. Login or sign up in order to post.