Error consulting text with emojis in mysql

2

Update ...
I have the following table:

CREATE TABLE 'chrislat_cms'.'opciones' (
'id' INT(11) NOT NULL AUTO_INCREMENT ,
'nombreOpcion' VARCHAR(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ,
'valorOpcion' LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ,
PRIMARY KEY ('id')
) ENGINE = MyISAM CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;

With the following data:

| id | nombreOpcion | valorOpcion |
|----|--------------|-------------|
| 1  | nombre_sitio |  cms-CLT                     
asked by Christopher Villa 29.05.2018 в 06:22
source

2 answers

1

Since the complañero answered with an example in PDO when you use MySQLi, I will leave this answer with an example in MySQLi.

The first thing you can see in this response from A. Cedano , is to check that everything is in accordance with the desired coding, generated document (html, js, css ...), web server, connections database server , database, tables, columns ... In short, all parties involved should be in the same coding.

Finally, let's see an example:

// Establecer codificación en las cabeceras enviadas para la petición http
header('Content-Type: text/html; charset=UTF-8');

// Conexión
$cCms= new mysqli("localhost", "mi_usuario", "mi_contraseña", "test");

// Estabecer codificación para la conexión
if (!$cCms->set_charset("utf8mb4")) {
    printf("Error cargando el conjunto de caracteres utf8: %s\n", $cCms->error);
    exit;
} 

$consulta = "select valorOpcion from opciones where nombreOpcion='$nombre'";
$hacerConsulta = mysqli_query($cCms, $consulta) or die(mysqli_error($cCms));
if($hacerConsulta){
    if(mysqli_num_rows($hacerConsulta)==1){
        $fila = mysqli_fetch_array($hacerConsulta);
        $valor = $fila['valorOpcion'];
    }else if(mysqli_num_rows($hacerConsulta)>1){
        $valor = "Error al obtener datos de $nombre [+1]";
    }else{
        $valor = "Error al obtener datos de $nombre [0]";
    }
}else{
    $valor = "Error al obtener datos de $nombre [-1]";
}

echo $valor;
    
answered by 29.05.2018 / 11:17
source
1

I leave the solution to your question, but done with PDO instead of mysqli with a sample of the page working

page index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php

$conexion = new PDO("mysql:host=localhost;port=3307;dbname=demo;charset=utf8mb4", "root", "khe");

$consulta = $conexion->prepare("select * from msg");
$consulta->execute();
$resultado = $consulta->fetchAll();


foreach ($resultado as $row) {
            echo $row["message"];
        }
?>
</body>
</html>

For the case of the database once you declared the table that will contain the emojis, execute the following command

SET NAMES utf8mb4;

For the case of the table, we will set the character set as follows

ALTER TABLE msg CONVERT TO CHARACTER SET utf8mb4;

In the connection as you can notice I also declare the character set utf8mb4

The following image shows the final result on the website

    
answered by 29.05.2018 в 07:25