How to convert latin1_swedish_ci data to utf8_bin in php?

Asked

Viewed 775 times

3

I got an SQL script that "mounts" 3 tables, one with country, one with states and one with Brazilian cities. The script makes the collection, of the names, in latin1_swedish_ci and I needed it to be utf8_bin, because names with accent generates another character, and I still have to put these names in format json.

My HTML:

<div align="center">
<form>
    <input type="hidden" ng-model="estado.idEstado">
    Estado <input type="text" ng-model="estado.nome">
    Sala <input type="text" ng-model="estado.sala">
    <button ng-click="atualizarEstado(estado)">Atualizar</button><br>
</form>

My php:

<?php
include_once("conPDO.php");
$pdo = conectar();

$id = $_GET['idEstado'];

$buscarEstado=$pdo->prepare("SELECT * FROM estado WHERE idEstado=:id ");
$buscarEstado->bindValue(":id", $id);
$buscarEstado->execute();

//header('Content-Type: application/json');

$return = array();

while ($linha=$buscarEstado->fetch(PDO::FETCH_ASSOC)) {
        $return = $linha;
        print_r($return);
}

echo json_encode($return);
?>

On the console appears this:

Array
    (
        [idEstado] => 4
        [nome] => Amap�
        [uf] => AP
        [pais] => 1
        [sala] => 
    )
  • Test do the following $return = utf8_encode($linha); and json_encode($return JSON_UNESCAPED_UNICODE);. See if it solves.

  • This message appeared on the console: "<b>Warning</b>: utf8_encode() expects Parameter 1 to be string, array Given in <b>/Applications/MAMP/htdocs/systems/systems_web/Vigilantescomunitarios/admin/php/pegaEstado.php</b> on line <b>22</b><br />"

  • Then just try echo json_encode($return JSON_UNESCAPED_UNICODE); Leave Return as it was.

  • Nothing... Shows this error on the console: "GET http://localhost:8888/systems/systems_web/Vigilantescomunitarios/admin/php/pegaEstado.php? idEstado=4 500 (Internal Server Error)"

  • All right, test that echo json_encode($return, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); with this he will try to do all the decodes he has.

  • Thanks for your attention @Moisesgama, but I already did. I did this: $line['name'] = mb_convert_encoding($line['name'], "ISO-8859-1");

  • Gave another problem... The name, now, is displayed, in the listing, this way Amapã¡ instead of Amapá. And if I try to edit to correct, it no longer appears in the edit field.

  • @Moisesgama, I did this: $line['name'] = json_encode($line, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE); array_push($line, $line['name']); $Return = $line; And this appeared on the console: "Array ( [idEstate] => 4 [name] => [Uf] => AP [parents] => 1 [room] => Eama4 [0] => ) {"idState":"4","name"false,"Uf"""AP","parents":"1","room":"Ea4","room","}"

  • put this before the include header("Content-type: text/html;charset=utf-8"); and see how it prints on print_r($return); without putting in the JSON

  • Before doing what you suggested to me, I did it in a way that is working, HOWEVER, when I update the data, the name comes "broken" hehehe ?

Show 5 more comments

1 answer

1

Run a query by setting the database charset

$pdo->query('SET NAMES UTF-8');

or if your bank is using ISO-8859-1

$pdo->query('SET NAMES LATIN1');

Browser other questions tagged

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