Update by json does not recognize UTF8 by database

0

When I press the LOAD button in my portfolio, $ NAME and $ TYPE bug when there is some kind of accent. The first 6 blocks of the portfolio that are already loaded with the page, are in UTF8 but the others are not. Check out the site: somospixel.com/test in the PORTFOLIO part

<?php

function fn_conexao(){

    $dbuser = "######";
    $dbpass = "#####";

    try {

        $pdo = new PDO('mysql:host=#####;dbname=#####',  $dbuser, $dbpass);
        $pdo -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
        $pdo->exec("SET CHARACTER SET utf8");//corrige os acentos na hora de gravar no BD
    } catch(Exception $e) {

        echo 'Erro na conexao: ' . $e->getMessage();
    }

    return $pdo;
}

function dados($pdo){

    try {   

            if(!isset($_GET['id']) or $_GET['id'] == null){

                $id = 0; //se o GET nao for enviado o for enviado como nullo , a variável ID pega o valor de 0

            }else{

                $id = $_GET['id']; //pega o valor passado via GET
            }

            $arr = array();

            $sql = "ALTER DATABASE pot CHARSET = UTF8 COLLATE = utf8_general_ci";
            $sql = "SELECT * FROM pot WHERE id < $id ORDER BY id DESC LIMIT 6";
            $stmt = $pdo->prepare($sql);
            $stmt->execute();
            $linha = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if($stmt->rowCount() >= 1){

                return $linha; //retorna o resultado da query

            }else {

                return 0;

            }
        } catch(Exception $e) {

            print 'Erro ao inserir os dados no banco: ' . $e->getMessage();
            $conexao = desconecta($conexao);

        }
}

$conexao = fn_conexao();
$dados = dados($conexao);

$dados = json_encode($dados); //converte o resultado para json

print $dados; //imprime os dados na tela
?>
    
asked by anonymous 17.03.2016 / 21:52

1 answer

0

Just remove the

$pdo->exec("SET CHARACTER SET utf8");//corrige os acentos na hora de gravar no BD

I was doing the conversion twice, so he bugged

    
21.03.2016 / 21:19