Accent error in PHP

Asked

Viewed 118 times

-1

I’m having stress problems in PHP.

In the database is inserted with the right accent, but when I return to PHP the accent is not returned correctly, someone can help me?

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="estilo.css">
</head>
<body>
<div class="busca">
    <form action="">
    <input type="search" name="busca" placeholder="Buscar" id="busca" required>
    <input type="submit" value="Buscar" name="btn" id="btn">
    </form>
    <?php
    $conexao = mysqli_connect("localhost","root","","dict2");
    $busca = isset($_GET['busca'])?$_GET['busca']:"";
    if (isset($_GET['btn'])){
        $sql = "select * from tb_dict2 where def LIKE '%$busca%' OR trad LIKE '%$busca%'";
        $result = mysqli_query($conexao,$sql);
        while ($linha = mysqli_fetch_array($result)){
            $trad = $linha['trad'];
            $def = $linha['def'];
            echo "<div class='content'>";
            echo "<h3>Português: ".$def."<br>";
            echo "Indonésio: ".$trad."<br>";
            echo "</div";
        }
    }
    ?>
</div>
</body>
</html>

1 answer

0

Try setting your characters to utf8 by mysqli command.

http://php.net/manual/en/mysqli.set-charset.php

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="estilo.css">
</head>
<body>
<div class="busca">
    <form action="">
    <input type="search" name="busca" placeholder="Buscar" id="busca" required>
    <input type="submit" value="Buscar" name="btn" id="btn">
    </form>
    <?php
    $conexao = mysqli_connect("localhost","root","","dict2");
    /* mudar o character set para utf8 */
    if (!$conexao->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $conexao->error);
        exit();
    } else {
        printf("Current character set: %s\n", $conexao->character_set_name());
    }
    $busca = isset($_GET['busca'])?$_GET['busca']:"";
    if (isset($_GET['btn'])){
        $sql = "select * from tb_dict2 where def LIKE '%$busca%' OR trad LIKE '%$busca%'";
        $result = mysqli_query($conexao,$sql);
        while ($linha = mysqli_fetch_array($result)){
            $trad = $linha['trad'];
            $def = $linha['def'];
            echo "<div class='content'>";
            echo "<h3>Português: ".$def."<br>";
            echo "Indonésio: ".$trad."<br>";
            echo "</div";
        }
    }
    ?>
</div>
</body>
</html>

Browser other questions tagged

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