mysql PHP problem when using trema

Asked

Viewed 131 times

1

I need to develop a script that checks for an accented character with trema in the string and saves a string variable without the trema.

The script I list below compiles

//Checa Especial
$nomeCliente = "Bröring";

if(strstr($nomeCliente, "ä") || 
   strstr($nomeCliente, "ö") || 
   strstr($nomeCliente, "ü")){

    if(strstr($nomeCliente, "ä")){ $variavel .= str_replace("ä", "a", $nomeCliente).","; }
    if(strstr($nomeCliente, "ö")){ $variavel .= str_replace("ö", "o", $nomeCliente).","; }
    if(strstr($nomeCliente, "ü")){ $variavel .= str_replace("ü", "u", $nomeCliente).","; }

    echo $variavel;
    //Imprime Broring,

}else{
    echo "Não há trema";
}

I did a direct query in Phpmyadmin

SELECT id, nome FROM clientes WHERE nome LIKE '%ö%'

To see if you could pull the data like this and compile inserir a descrição da imagem aqui

So I implemented a script

//Gera variáveis do nome
$sqlTremas = mysqli_query($conn, "SELECT id, nome FROM clientes WHERE nome LIKE '%ö%' OR nome LIKE '%ä%' OR nome LIKE '%ü%'");

while($dadosT = mysqli_fetch_array($sqlTremas)) {
    //Checa Especial
    $nomeCliente = utf8_decode($nomeCliente);

    if(strstr($nomeCliente, "ä")){ $variavel .= str_replace("ä", "a", $nomeCliente).","; }
    if(strstr($nomeCliente, "ö")){ $variavel .= str_replace("ö", "o", $nomeCliente).","; }
    if(strstr($nomeCliente, "ü")){ $variavel .= str_replace("ü", "u", $nomeCliente).","; }

    $tamanhoStringVariavel = strlen($variavel);
    $variavel = substr($variavel, 0, $tamanhoStringVariavel-1);

    $alteraCliente = mysqli_query($conn, "UPDATE clientes SET variavel = '{$variavel}' WHERE id = {$dadosT['id']}");

    if($alteraCliente) {
        echo "SUCESSO {$dadosT['id']}<br><br>";
    }else{
        echo "ERRO {$dadosT['id']}<br><br>";
    }
}

In that code the mysql_num_rows($sqlTremas) comes empty, but when I compile directly in mysql, it works. I have another query in the same file that works (not connection to the database). What can it be?

  • Of curiosity, what is the problem of having tremble in the DB string? It would not be better to simply keep it as it is, and hit the collation and the charset of the bank so that it works all regardless of the accentuation?

  • Hi @Bacco, none. I keep the shakes in the bank normally, but in a search for this project I need the customer to find the result if you write Bröring or Broring, for example.

  • Possibly in Insert you use utf8_encode and in comparison you decode string with uf8_decode as in utf8 the characters are saved encoded your sql will not work.

  • But this is the normal one. If you are not finding it, the problem is wrong configuration. Using the correct charset, and the Swedish collation, JOÃO == Joao. What you need to see is whether you want it compatible or basic, which can turn "ö" into "Oe" or just "o". Even, Bröring is the same as Broering in principle. Whether one way or another, the solution is in the collation from the bank. This which was searched in the question is kind of "patch" the thing. I would suggest trying to solve directly in the bank, without changing the string.

  • @Bacco I am using colation latin1_swedish_ci, without the trema the like does not find the direct result in the sql of phpmyAdmin. I must use another?

  • if it is latin1, it has to be charset latin1. if you want utf8 in the charset, use a collation utf8 as well. This mixture can be the cause of the problem.

  • @Bacco a crossbow doubt: can changing the collation on the bank alter somehow my records? Another question: the John and John thing works. I mean, the accents are just the shakes the problem. Isn’t it weird? So I figured the problem was with the shakes anyway.

  • Always work with a copy. Changing your original data can give you the most confusion if you don’t have practice with collation. Then it becomes difficult to fix, as it can mix old data into one format and new data into another. I suggest you try to change it, but only after you make sure you can go back if you make any mistakes. About the trema, test as I said, see if you find "broering" having "bröring" in the bank. If so, you can exchange for the simplest international, which does not change the umlauts

  • Related :D https://www.youtube.com/watch?v=FzK6VdK8YRs

Show 4 more comments

1 answer

0

Have you checked the excerpt below the comment *//Checa Especial*

$sqlTremas = mysqli_query($conn, "SELECT id, nome FROM clientes WHERE nome LIKE '%ö%' OR nome LIKE '%ä%' OR nome LIKE '%ü%'");

while ($dadosT = mysqli_fetch_array($sqlTremas)) {
    //Checa Especial
    $nomeCliente = ($dadosT['nome']);

    if (strstr($nomeCliente, "ä")) {
        $variavel .= str_replace("ä", "a", $nomeCliente) . ",";
    }
    if (strstr($nomeCliente, "ö")) {
        $variavel .= str_replace("ö", "o", $nomeCliente) . ",";
    }
    if (strstr($nomeCliente, "ü")) {
        $variavel .= str_replace("ü", "u", $nomeCliente) . ",";
    }

    $tamanhoStringVariavel = strlen($variavel);
    $variavel = substr($variavel, 0, $tamanhoStringVariavel - 1);

    $alteraCliente = mysqli_query($conn, "UPDATE clientes SET variavel = '{$variavel}' WHERE id = {$dadosT['id']}");

    if ($alteraCliente) {
        echo "SUCESSO {$dadosT['id']}<br><br>";
    } else {
        echo "ERRO {$dadosT['id']}<br><br>";
    }
}
  • It doesn’t even enter there, because there is no return in the query via PHP. But if my query in PHP is '%o%' OR LIKE name '%a%' OR LIKE name '%u%' compiles.

Browser other questions tagged

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