PHP accent problem, Mysql

Asked

Viewed 2,045 times

4

I have a very strange problem with my PHP code.

All my charset are correct

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

I already use the code below to correct accent problems

header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

When I send the form to Mysql it writes to the database correctly (latin1_swedish_ci database).

Everything appears perfectly on the screen.

The problem appears when I make a str_replace. When I take the URL variable by the $_GET method (which is with correct accent) and put it in the str_replace function, the function does not recognize the letter with accent and does not change. Now, if I write in my code the same word ( <?php $str = "joão" ?> for example ), hence the function recognizes the accent.

The problem also happens when the code compares a word with what is in the database. If he compares a letter with an accent that is in the code with a word that has an accent in the bank, he does not recognize it (then returns no result).

I’m using str_replace to search with REGEXP, so my code takes an accent word, swaps it for an equal word without accent (here I already have the error) and then creates a string with REGEXP to search.

Please help that I no longer know where to look!

  • Do not use two goal tags for the same purpose, the second is the improved version (HTML5) of the first. Only it already solves.

  • Friendly advice: check the files. Probably your files are encoded as iso8859-1, a good text editor like Sublime Text, Atom etc allows you to convert. About the database, if in the database you use iso8859-1 just remember to put the same ISO in the mysql_query. It is not "gambiarra" as the colleague said, it is an automatic PHP conversion system, which works very well if you know what you are doing. Just specify in PHP the same Sets pro database, table, fields etc. that you used in the database, and they convert everything perfectly.

2 answers

3

Database latin1_swedish_ci should not be used with utf8, if not you will stay in the gambiarra to solve the problem.

Bank latin1, then use on the page <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

This problem occurs because the accented characters of utf8 are totally different from latin1, so for the bank are two different things.

If you will use pages in utf-8, then use the bank in utf-8, recommend reading this:

  • I changed my bank to utf8, and even then it didn’t help. As commented above, I also found it strange but the url is with accent! Could it be a problem with php.ini? I tried to change some settings and it didn’t work.

  • @Gabrieloliveira Did you use ALTER TABLE sua_tabela CONVERT TO CHARACTER SET utf8 COLLATE charset_escolhido_por_você;? Which charset you used?

0

An alternative way is that if we’re using PDO, the fourth parameter at the time of instantiating the PDO class is super important for special character situations.

//Fazemos a conexão com nosso banco de dados

$dsn = "mysql:dbname=nome_do_banco;host=localhost"; 
$dbuser = "root"; //usuário do banco
$dbpass = ""; //senha do banco
try
{
    //Instanciamos a classe PDO passando como parâmetro os dados de conexão pegos acima
     $pdo = new PDO($dsn, $dbuser, $dbpass,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); 
    //Esse quarto parâmetro é para corrigir problemas de acentuação

}
catch(PDOException $e)
{   
   //Se der erro na conexão, nós estourando esse Catch e é exibida essa mensagem junto com a mensagem do erro
   echo "A conexão falhou: ".$e->getMessage();
   exit;
}

That is the parameter I refer to:

array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')
  • In addition, of course, to leave the COLLATE and CHARSET of the database and the tables of the database configured in the correct way.

Browser other questions tagged

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