This topic specifically talks about UTF-8 and considerations for using it with a database. If you want more information about using databases in PHP then checkout this topic.
Storing Data in a MySQL Database:
utf8mb4character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8.
MySQL will implicitly use
utf8mb4encoding if a
utf8mb4_*collation is specified (without any explicit character set).
utf8mb4so you'll be forced to use
utf8, which only supports a subset of Unicode characters.
Accessing Data in a MySQL Database:
In your application code (e.g. PHP), in whatever DB access method you use, you'll need to set the connection charset to
utf8mb4. This way, MySQL does no conversion from its native UTF-8 when it hands data off to your application and vice versa.
Some drivers provide their own mechanism for configuring the connection character set, which both updates its own internal state and informs MySQL of the encoding to be used on the connection. This is usually the preferred approach.
For Example (The same consideration regarding
utf8 applies as above):
$handle = new PDO('mysql:charset=utf8mb4');
$conn = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); $conn->set_charset('utf8mb4'); // object oriented style mysqli_set_charset($conn, 'utf8mb4'); // procedural style
$conn = mysql_connect('localhost', 'my_user', 'my_password'); $conn->set_charset('utf8mb4'); // object oriented style mysql_set_charset($conn, 'utf8mb4'); // procedural style
If the database driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded:
SET NAMES 'utf8mb4'.