CodeIgniter Forums
Emojis - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Emojis (/showthread.php?tid=86249)



Emojis - gregknight - 01-23-2023

I have an old PHP app that has an email feature.  The problem we are having is the Subject and Message boxes in the email feature will not handle saving emojis to the database or retrieving emojis from the database.  I currently am just setting the subject column in the database to 5 emojis and the word "test" for testing purposes, so I know what I'm working with.  Here is the code doing the insert into the table.  I have set the char_set and dbcollat in the database.php file, but it didn't work, so I've also tried setting it here in the code right before the insert.  It doesn't work either.

        //echo("<script>console.log('PHP MailboxModel saveMessage " . $data['message'] . "');</script>");

        $db['default']['char_set'] = "utf8mb4";

        $db['default']['dbcollat'] = "utf8mb4_unicode_ci";

        $query = $this->ci->db->query("SET character_set_connection=utf8mb4");

        $query = $this->ci->db->query("SET character_set_results=utf8mb4");

        $data['subject'] = '❤️???❤️test';

        if (is_null($message_id)) {

            if (!isset($data['date_add'])) {

                $data['date_add'] = date('Y-m-d H:iConfused');

            }

            if (!isset($data['is_new'])) {

                $data['is_new'] = 1;

            }

         

            $this->ci->db->insert(MAILBOX_TABLE, $data);

            $message_id = $this->ci->db->insert_id();

        } else {

            $this->ci->db->where('id', $message_id);

            $this->ci->db->update(MAILBOX_TABLE, $data);

        }



        $this->updateFulltextField($message_id);



        return $message_id;

    }





Here are the rows that were saved to the database.  Notice that the Subject column has ?? instead of the emojis in the middle.



[Image: 67b73d_k8u]
Subject
❤️????????????❤️test


I believe the code is using CodeIgniter version 1.7.0.
// CI Version

define('CI_VERSION', '1.7.0');

Does CI version 1.7.0 even support the inserting and retrieving of emojis into and from the database?


RE: Emojis - InsiteFX - 01-24-2023

You can get the CI version using echo CI_VERSION;

You may need to use php htmlspecialchars() and php htmlentities()

to convert them, run them on the full strings.


RE: Emojis - gregknight - 01-24-2023

(01-24-2023, 01:52 AM)InsiteFX Wrote: You can get the CI  version using echo CI_VERSION;

You may need to use php htmlspecialchars() and php htmlentities()

to convert them, run them on the full strings.

This made no difference on what appeared in the database.  Sad


RE: Emojis - kenjis - 01-24-2023

MySQL table has its charset. If it is not utf8mb4, you cannot save emojis correctly.


RE: Emojis - gregknight - 01-25-2023

(01-24-2023, 05:14 PM)kenjis Wrote: MySQL table has its charset. If it is not utf8mb4, you cannot save emojis correctly.
I only see a collation for the table and it is set to utf8mb4_unicode_ci.


RE: Emojis - gregknight - 01-25-2023

I've tried everything I know of and it just doesn't work. I'm guessing that CI 1.7.0 didn't support emojis..... So, how hard is it to upgrade very old code to the newer version of CI???


RE: Emojis - InsiteFX - 01-26-2023

You also need to set it in the app/Config/Database.php file.
PHP Code:
  /**
    * The default database connection.
    */
    public array $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'database' => '',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => true,
        'charset'  => 'utf8mb4',
        'DBCollat' => 'utf8mb4_unicode_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'    => 3306,
    ]; 



RE: Emojis - gregknight - 01-27-2023

(01-26-2023, 01:07 AM)InsiteFX Wrote: You also need to set it in the app/Config/Database.php file.
PHP Code:
  /**
    * The default database connection.
    */
    public array $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'database' => '',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => true,
        'charset'  => 'utf8mb4',
        'DBCollat' => 'utf8mb4_unicode_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'    => 3306,
    ]; 

I have it set in the database.php.  I've done everything and it still doesn't work.  I don't think emojis were supported in 1.7.0.....


RE: Emojis - InsiteFX - 01-28-2023

I found this according to this you need to use a BLOB character type for the column.

How to store Emoji Character in MySQL Database

Should help you.