How to convert latin1_swedish_ci data to utf8_bin in php?

3

I've got a 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 the names with accent generates another character, and I still have to put those names in json format. >

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 this appears:

Array
    (
        [idEstado] => 4
        [nome] => Amap�
        [uf] => AP
        [pais] => 1
        [sala] => 
    )
    
asked by anonymous 13.01.2016 / 15:01

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');
    
14.01.2016 / 12:36