9951 explained code solutions for 126 technologies


php-mysqlHow to use utf8mb4_unicode_ci in MySQL with PHP?


MySQL supports the utf8mb4_unicode_ci character set, which allows for storing characters from a wide range of languages. To use this character set in PHP, you need to set the character set in the connection string when connecting to the database.

$conn = new mysqli("localhost", "username", "password", "database", 3306, "", MYSQLI_CLIENT_SSL);
$conn->set_charset("utf8mb4_unicode_ci");

The code above creates a new connection to the database and sets the character set to utf8mb4_unicode_ci.

Code explanation

  1. $conn = new mysqli("localhost", "username", "password", "database", 3306, "", MYSQLI_CLIENT_SSL); - creates a new connection to the database.
  2. $conn->set_charset("utf8mb4_unicode_ci"); - sets the character set to utf8mb4_unicode_ci.

Helpful links

Edit this code on GitHub