Save Accents in MySQL Database

The best bet is that your database connection is not UTF-8 encoded - it is usually ISO-8859-1 by default.

Try sending a query

SET NAMES utf8;

after making the connection.


if you use PDO, you must instanciate like that :

new \PDO("mysql:host=$host;dbname=$schema", $username, $password, array(\PDO::MYSQL_ATTR_INIT_COMMAND =>  'SET NAMES utf8') );

Personally I solved the same issue by adding after the MySQL connection code:

mysql_set_charset("utf8");

or for mysqli:

mysqli_set_charset($conn, "utf8");

or the mysqli OOP equivalent:

$conn->set_charset("utf8");

And sometimes you'll have to define the main php charset by adding this code:

mb_internal_encoding('UTF-8');

On the client HTML side you have to add the following header data :

<meta http-equiv="Content-type" content="text/html;charset=utf-8" />

In order to use JSON AJAX results (e.g. by using jQuery), you should define the header by adding :

header("Content-type: application/json;charset=utf8");
json_encode(
     some_data
);

This should do the trick


mysqli_set_charset($conn, "utf8");