How to properly configure JSON return with content coming from Mysql with accent

Asked

Viewed 147 times

2

I have a problem in the return JSON of some information from my BD, some are accentuating and the answer fails, I have tried some past suggestions, such as this:

$response = utf8_encode($response);

But it didn’t, what I have is this:

if(isset($_POST['Historico']) && isset($_POST['Historico']) != "") {

    $IdHistorico = $_POST['Historico'];

    // ÁREAS x EXPECTATIVA
    $sqlHistoricoProfissional = "SELECT 
                                  IdHistorico,
                                  IdCandidato,                            
                                  Empresa, 
                                  Cargo, 
                                  date_format(crrHistoricoProfissional.DataEntrada, '%d/%m/%Y') AS DataEntrada, 
                                  date_format(crrHistoricoProfissional.DataSaida, '%d/%m/%Y') AS DataSaida, 
                                  UltimoSalario,
                                  Atividades
                             FROM crrHistoricoProfissional WHERE IdHistorico = ?";
    $stm = $conexao->prepare($sqlHistoricoProfissional);
    $stm->bindValue(1, $IdHistorico, PDO::PARAM_INT);
    $stm->execute();    
    $sqlHistoricoProfissional = $stm->fetchAll(PDO::FETCH_OBJ); 
    // CONTAGEM DE REGISTROS RETORNADOS
    $ContReg = count($sqlHistoricoProfissional); 
    // FECHANDO A CONSULTA
    $stm->closeCursor();    

    $response = array();

    if($ContReg > 0) {

        foreach ($sqlHistoricoProfissional as $DadosReg) {
            $response = $DadosReg;      
            // $response = utf8_encode($response);
            //var_dump($row);
        }

    } else {
        $response['status'] = 200;
        $response['mensagem'] = "Não foram encontrados registros";
    }
    // display JSON data
    echo json_encode($response);
} else {
    $response['status'] = 200;
    $response['mensagem'] = "Requisição inválida";
}

My connection is like this:

define('HOST', 'localhost'); 
define('USER', 'xxxxxxx'); 
define('PASSWORD', 'xxxxxxx'); 
define('DBNAME', 'xxxxxxx'); 
define('TYPEDB', 'mysql'); 
define('CHARSET', 'utf8');

The Bank is like this:

COLLATE='utf8_general_ci'
ENGINE=InnoDB
  • 1

    tried it like this ? json_encode($response, JSON_UNESCAPED_UNICODE);

  • Hello @Matthew, already but not in this format, it was perfect, thank you very much, as I can give an acceptance?

1 answer

1


Try a return similar to that :

json_encode($response, JSON_UNESCAPED_UNICODE);

Browser other questions tagged

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