2

I've searched for about the last 3 hours for a solution, but it doesn't work.

MySQL doesn't support utf8mb4 (this is one solution I can't test).

Thank you!

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • "MYSQL doesn´t support utf8mb4" - but it does [as of version 5.5.3](https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html). – VolkerK Feb 28 '16 at 22:29
  • Possible duplicate of [MySQL utf8mb4, Errors when saving Emojis](http://stackoverflow.com/questions/35125933/mysql-utf8mb4-errors-when-saving-emojis) – Anthony Feb 28 '16 at 23:57

2 Answers2

2

Here is an example DB connection using PHP and MySQL 5.5.3:

public function_construct($host, $db, $user, $pass){
    try {
      $this->conn = new PDO("mysql:host = {$host};
                             dbname = {$db};
                             charset = utf8mb4",
                             $user,
                             $pass);
    } else { exit(); }
}

As of release 5.5.3 utf8mb4 is fully backwards compatible with utf8. If you are working with an existing database look for the MySQL configuration file and change instances of 'utf8' to 'utf8mb4' accordingly.

oliver
  • 2,467
  • 3
  • 14
  • 29
  • 2
    And please keep in mind that [5.5.3/5.5.4 was released in Q2 of 2010](https://dev.mysql.com/doc/relnotes/mysql/5.5/en/); that's almost 6 years ago. There _might_ be a reason for still running an older version, possibly even a good one, but unless stated otherwise there's no reason for "us" to think that this is the case. I.e. if using utf8mb4 is not an option, you (Martin Rauhut) should explicitly say so (and maybe also explain why, to avoid uncessary discussions about it). – VolkerK Feb 28 '16 at 22:44
  • why is this answer selected? the question is about MySQL, not PHP – devnull Ψ Oct 15 '18 at 09:04
  • This illustrates adding the proper charset, using PHP is incidental to the content of the answer, it applies generally – oliver Oct 15 '18 at 22:47
1

Use Mysql blob field, save images directly there, although I personally don't like saving images in DB, save links to files instead.

$img = mysql_escape_string(file_get_contents('imagefile.gif'));

Then you would insert this $img into db.

Muhammed
  • 1,592
  • 1
  • 10
  • 18
  • 1
    I don't think this is about images. e.g. is a "character", it's the [code point U+1F385](http://unicode-table.com/en/1F385/). Its utf-8 (hex) representation is `F0 9F 8E 85`, i.e. a 4-byte sequence. But MySQL's utf8 charset is limited to 3-byte sequences at max, while utf8mb4 _can_ encode/handle such sequences. – VolkerK Feb 28 '16 at 22:32