PHP: query with Latin characters returns nothing

Asked

Viewed 39 times

3

Hello, I am writing a code from a page that will make a query in a database using data in a form with two inputs: matricula and name.

The idea is this:

If the user does not enter anything and click on the query button, the whole table of the bank will be printed, what is working, if the user type the registration, the query will be made by the registration, this part is also working, if the user only type the name, a query will be made by name, however, this query is only returning results if there are no Latin characters.

For example, if php sends a query

SELECT matricula, nome, turma FROM alunos WHERE nome LIKE '%JOSÉ%'

will not return anything, however, when I run this same query directly in the database, it returns me results, which indicates that the problem is in php, someone knows tell me where the problem is?

Below is the code, the crucial part is the function imprimirFiltro();

   <?php
    $servidor = "localhost";
    $usuario = "root";
    $banco = "lyceu_secretaria";

    $conn = new mysqli($servidor, $usuario, "", $banco);


    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    if (empty($_POST)) {
        imprimirTudo();

        } else{
            imprimirFiltro();
    }

    $conn->close();

    function imprimir($sql){
        global $conn;
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {

            echo "<table class = 'table'><tr> <th>Matricula</th> <th>Nome</th> <th>Série e turma atual</th> 
                <th></th> </tr>";
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "<tr><td>". $row["matricula"] ."</td><td>". utf8_encode($row["nome"])."</td><td>".
                        $row["turma"] . "</td><td>" . 
                        "<a href = 'fichaaluno.php?matricula="
                            .$row["matricula"]." '<button type= 'button' class = 'btn btn-info'> 
                            <span class='glyphicon glyphicon-file' aria-hidden='true' style='color:white'></span>
                                Gerar Ficha Completa </button></a>

                        <a href = 'editaraluno.php?matricula="
                            .$row["matricula"]." '<button type= 'button' class = 'btn btn-primary'> 
                            <span class='glyphicon glyphicon-pencil' aria-hidden='true' style='color:white'></span>
                                Editar </button></a>

                        </td></tr>" ;
                }
                echo "</table>";
            } else {
                echo "Sem resultados " . $sql;
        }
    }
    function imprimirTudo(){
        $sql = "SELECT matricula, nome, turma FROM alunos ORDER BY nome ASC ";

        imprimir($sql);
    }

    function imprimirFiltro(){

        $matricula = $_POST['matricula'];
        $nome = mb_strtoupper(utf8_encode($_POST['nome']),'UTF-8');     

        if ($matricula != "") { 

            $sql = "SELECT matricula, nome, turma FROM alunos WHERE matricula = $matricula ";
            imprimir($sql);

            } else if($nome != "") {

                $sql = "SELECT matricula, nome, turma FROM alunos WHERE nome LIKE '%$nome%'";
                echo "Resultados da pesquisa para: " . $nome ."<br />";
                imprimir($sql);
            } else{ //PERMITE QUE TODA A LISTA SEJA IMPRESSA SE NENHUM CAMPO DO FORMULÁRIO É PREENCHIDO
                imprimirTudo();
        }               
    } ?>
  • Probably, PHP is not passing "JOSÉ" for the query but something like "JOS &" or whatever. Solutions: open all your files in the notepad and see if they are ANSI, if so, save them as UTF-8; if not, try encoding in connection with the database http://php.net/manual/en/mysqli.set-charset.php. Avoid using PHP functions utf8_encode() and utf8_decode().

  • Using the encoding on the connection worked, thank you. But where it has $name = mb_strtoupper(utf8_encode($_POST['name']),'UTF-8'); had to stay with utf8_encode() if I remove it php sends the name as JOS?. But it was a precious tip, I realized that to print on the screen I no longer need to use utf8_encode, since the database already returns the result of select in this encoding, thanks.

  • 1

    Fixing, I managed to get the utf8_encode() from $name, I just had to go in the form of the html that was with Accept-charset="ISO-8859-1" then I switched to Accept-charset="UTF-8"

  • So it’s all right now?

  • Yes, and besides having solved this problem, probably the tip you gave me will help me in other parts of the system I am developing, thank you very much.

  • On the form page, it is preferable to use meta tag <meta charset="utf-8>" than the attribute accept-charset="UTF-8" form. Avoid using as much as possible ISO-8859-1.

  • 1

    Have you found a solution? It’s like the duplicate marked here?

  • 1

    No, it is not a duplicate, my doubt refers only to sending a query select with Latin characters, that other doubt is about these characters not appearing on the page, in my case they appear, the problem was only the way php sent them to mysql.

Show 3 more comments
No answers

Browser other questions tagged

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