MySQL (MariaDB) saves everything in UTF8 correctly, but PHP prints it wrong. Why? [duplicate]

3

I'm making PHP, configured in default_charset = UTF-8 , put data brought from the MySQL database, also configured with all character variables and collation in UTF-8, within a select element, but it does not correctly encode characters with accents, as seen in this image:

The problem is that everything that PHP prints that has nothing to do with data from MySQL does print correctly encoded. Likewise the data of the base are well kept with their accents and others.

When I use the utf8_encode function, the problem is solved. But we all know how exhausting it is to invoke the function for each variable. And even more so when it's not necessary knowing that Apache Tomcat, PHP, and MariaDB are all configured in UTF-8, so I do not understand.

Thank you very much for your cooperation!

    
asked by NicoleJuli 28.04.2017 в 22:09
source

2 answers

0

This code tells MySQL that it must use UTF-8 to correctly "interpret" the unicode characters.

$Conexion   = @new mysqli($Server,$User,$Password,$dBase,3306);
if (mysqli_connect_errno()) {
    echo "Error al conectar";
    exit();
}else{
    $SQL = "set names 'utf8';";
    $Resultado = $Conexion->query($SQL);
}
  

Remember that your PHP file must have the format UTF-8 or UTF-8 without BOM, if it is in ANSI, probably the problem will continue.   And if your page has HTML headers, then in the head use <meta charset="utf-8">

    
answered by 29.04.2017 в 00:22
0

I guess those results are printed in an HTML document because you do not try placing the meta in the header, it may work, I'm not sure

<head>
  <meta charset="UTF-8">
</head>
    
answered by 29.04.2017 в 01:00