Problems with special characters á, ç, à, etc with php/mysql

Asked

Viewed 2,270 times

0

Hello, I’m performing a get of files from the database to a page in order to be a search bar, but all results obtained are not accepting special characters, such as: ç, á, è.

ex: LEGISLATION O APLIC VEL

HTML/PHP:

<form action="search.php" method="POST">
      <div class="input-group mb-3">
        <input class="form-control" aria-label="Recipient's username" aria-describedby="basic-addon2" type="text" name="search" placeholder="Pesquisa...">
        <div class="input-group-append">
          <button class="btn btn-outline-secondary" type="button submit" name="submit-search">Buscar</button>
        </div>
      </div>
      </form> <br>
      <!-- Search bar -->

<?php
        $sql = "SELECT * FROM tablename";
        $result = mysqli_query($conexao, $sql);
        $queryResults = mysqli_num_rows ($result);

        if ($queryResults > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo "<div class='article-box'>
                    <h3>".$row['nome']."</h3>
                    <p>".$row['descricao']."</p>
                    <p>Relator: ".$row['relator']."</p>
                    <p>Data: ".$row['data']."</p>
                  </div>";
            }
        }
?>

Connection:

<?php
define('HOST', '127.0.0.1');
define('USUARIO', 'root');
define('SENHA', 'senha');
define('DB', 'dbname');

$conexao = mysqli_connect(HOST, USUARIO, SENHA, DB) or die ('Não foi possivel conectar!');

?>

Meta used in html:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

The collation of the database, connection and editor (Visual Studio Code) are:

utf8_general_ci

I tried to change the collation of everything to latin1_general_ci, but also failed.

The SET NAMES utf8 command did not result either.

At the head of html is in format <html lang="pt-BR">

I would like a light on this problem.

From now on, thank you for your patience!

  • @Pedroaugusto The solution of that other question did not result.

  • <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> try like this

  • @Mr Andrébaill There has been no change either

  • could show me your connection with the bank ?

  • @Paulovictor I may not have understood correctly, but is it the code that makes the connection to the bank? If so, I updated the question with the code.

  • @Andersoncarloswoss The solution was to add the charset config on the bank connection described in the other question, thanks for the information.

Show 1 more comment

2 answers

0

Try to use the commands

utf8_decode()
utf8_encode()

I worked on a system where every bank table had a different coding, what I had to do was utf8_decode(utf8_encode($texto))

  • Although I am using the same encoding for the entire database, I did not know this option given for the text, the problem was solved in the connection but I appreciate the information, I believe it will be useful in the future.

0


With the help of the comments I got a solution to this problem. Being it:

It was necessary to specify the charset in the database connection, along with the meta charset pattern already added in the question (being utf8).

Code line required:

if (!mysqli_set_charset($conexao, 'utf8')) {
    printf('Error ao usar utf8: %s', mysqli_error($conexao));
    exit;
}

I would like to thank everyone who has helped with the comments in the question, and for the possible duplicates that have solved the problem.

Thank you!

Browser other questions tagged

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