Home > other >  codeigniter 4 / how to select emoji from mysql db and display in view
codeigniter 4 / how to select emoji from mysql db and display in view

Time:01-11

I have a table called mh_country. Here is one row from phpmyadmin. You can see that emoji correctly displays the flag of Afghanistan.

snapshot of database to get emoji

The encoding in mysql of 'emoji' is correct and is as follows;

mysql structure of emoji

I would like to display the emoji in my view. First of all, here is my method to get 'emoji':

function display_country_dialing_codes(){
    $model = new \App\Models\MhCountryModel;
    $builder = $model->builder('mh_country');
    $builder->select('country_name, phonecode, emoji');
    $query   = $builder->get();
    $result  = $query->getResult();
    return $result;
}

Now I am trying to display the flag in my view with;

            $x = display_country_dialing_codes();
            echo $x[0]->emoji;

but all I get is '??'.

How can I display the emoji flag please?

CodePudding user response:

There is a quite simple solution to the problem:

instead of using varchar, change the datatype of emoji to blob or even tinyblob

images can be stored in a database either as a string (the image name, path, etc.) or in your case as a binary string (enter image description here

Edit your database config file

app\Config\Database.php

and set charset to utf8mb4 under your database connection

'charset'  => 'utf8mb4',
  •  Tags:  
  • Related