Why does the Inspector Character ( ) appear in some data obtained from the Database?

50

I was dealing with the dilemma of converting the accents and special characters of my system.

It happens that now some of the data obtained from the database that have tildes come out with this: .

The strange thing is that there can be up to 20 data displayed with accents but only some come out so SANCI�N that may be happening?

The only way is to place this <meta http-equiv="content-type" content="text/html; charset=UTF-8">

But despite being on the forms, the sigue

still comes out in some cases

The dynamically generated data gives that error

CONNECTION DATA:

config.ini

;<?php
;die(); // /* No modificar sino sabe lo que hace */
;/*
[database]
driver="mysql"
host="localhost"
port="3306"
schema="bbdd"
username="root"
password="pass" 
encode="utf8" 
;*/

Conexion.php :

<?php

<?php
$file = 'config.ini.php';
$config = parse_ini_file($file, true);
$host = $config['database']['host'];
$user = $config['database']['username'];
$pass = $config['database']['password'];
$schema = $config['database']['schema'];
$encode = $config['database']['encode'];
class conexion extends mysqli

    {
    public

    function __construct($host, $user, $pass, $schema)
        {
        parent::__construct($host, $user, $pass, $schema);
        if (mysqli_connect_error())
            {
            die();
            }
        }
    }

$conexion = new conexion($host, $user, $pass, $schema);
mysqli_set_charset( $conexion, $encode);
?>
    
asked by Victor Alvarado 31.03.2017 в 14:53
source

4 answers

63

When coding fails to present our information it is necessary to take a path back to the data to determine where the problem is.

When I say the way back, I want to say that we should start to review on the surface and go deeper, to refine the problem. An example of a return path would be:

Documento                          (HTML-Javascript-CSS, etc)   > Nivel 1
   Servidor                        (PHP u otro lenguaje)        > Nivel 2
      Conexión a la Base de Datos  (PDO, MySQLi u otros)        > Nivel 3 
          Configuración de la BD   (MySQL u otros)              > Nivel 4

I think it is an intelligent method of debugging, because in this case the data presented on our screen is taken from a database through a server language and presented on the screen. If we review in this order we will always get to the bottom of the problem , but by levels. It's logical: you can not get to the bottom without going through the surface :)

Also, here the most precious thing is the data , so the less we touch or alter it, the better. This also implies that it is very important when creating the database to properly configure the database itself, as well as each table and each column ... so we will not have to be manipulating its structure, which could be a risk especially if the database is already has information, because as we know we could lose it or cause errors in the data ... but that is another matter.

Then we start to check from the surface towards the bottom to see where the problem is.

Note: Before embarking on this return path, consider a possible revision Level 0 . It is possible that if we have worked our content in any text editor such as Notepad or others, the encoding of the document is not adequate. So, according to the editor, it would be convenient to verify the coding that has been established in our file. This is only in case we have taken the text of some editor.

This Level 0 is important, especially if we have badly configured the coding in the editor and we think we can continue to enter data in the database with a coding that is not what we need.

  • HTML settings

    • Place the following in <head> :

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

  • Server configuration (PHP or other)

    • In the case of PHP:

      We can use mb_internal_encoding :

      mb_internal_encoding("UTF-8");

      Or: default_charset (Since PHP 5.6+ is established to UTF-8 by default).

      ini_set("default_charset", "UTF-8");

  •   

    VERY IMPORTANT NOTE: Here I am referring to a configuration    global of PHP, not to apply coding / decoding functions on data constantly, which would be an error (which many   recommend, due to ignorance, in responses to similar cases   this). When we have problems with coding at the PHP level   we solve it at the root, in the configuration, not applying solutions to   half way that will keep us enslaved by applying   Constantly functions like utf8_encode to the data.

  • The way you connect to the BD, setting the encoding to UTF-8

    • In the case of MySQLi

      /* cambiar el conjunto de caracteres a utf8 */
      
      if (!$mysqli->set_charset("utf8")) {
          printf("Error cargando el conjunto de caracteres utf8: %s\n", $mysqli->error);
          exit();
      
      } else {
          printf("Conjunto de caracteres actual: %s\n", $mysqli->character_set_name());
      }
      

      See: mysqli :: set_charset

    • In the case of PDO

      We send the following attribute to the connection parameters: PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'" . Example:

         $options = array(
                PDO::ATTR_EMULATE_PREPARES => false, 
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
             );
         try {
               # Intentar la conexión 
               $pdo = new PDO($dsn, $usr, $pwd, $options);
         }
         catch (PDOException $e) {
               # Escribir posibles excepciones en el error_log
               error_log($e->getMessage(),0);
         }
         ...
      
  • The coding of the data itself (which can be: a, a column of a table, b, a table of the database, c, the database in general).

  • I would be debugging in that order, from 1 to 4.

    As you say that in some scenario is seen correctly, it seems that the problem is at level 3.

    Verify that when you connect to the Database you are establishing the encoding to UTF-8.

        
    answered by 31.03.2017 / 16:13
    source
    11

    Test as indicated by @A. Cedano

    You can also force UTF-8 from the .htaccess

    link

        
    answered by 01.04.2017 в 17:31
    3

    Greetings you have tried using utf8-default collation.

        
    answered by 13.02.2018 в 16:47
    1

    If you do not want to change the coding of your web page you can write the following, but without the points, since I put them so that the editor of the answer did not interpret them as characters:
    á - > &.; é - > &.; í - > &.; ó - > &. ú - > &.; ñ - >

    answered by 04.11.2018 в 01:05