Data Storage and Access

suggest change

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, see the other database topics in this book.

Storing Data in a MySQL Database:

MySQL will implicitly use utf8mb4 encoding if a utf8mb4_* collation is specified (without any explicit character set).

Accessing Data in a MySQL Database:

With PDO, specify the character set in the DSN when the connection is created:

$pdo = new PDO(
    'mysql:host=localhost;dbname=my_db;charset=utf8mb4',
    'my_user',
    'my_password',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
);

With MySQLi, select it through the driver API:

$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$connection->set_charset('utf8mb4');

The old mysql_* extension is unavailable in PHP 8 and must not be used.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents