+ 2
How to stop having ďż˝ in PhP and SQL.
Hello ! In my SQL queries on the my php page there are � which appear instead of "Ê or è...", I have nevertheless put my database in utf8mb4_unicode_ci and I put the meta tag : <meta charset="UTF-8">. Do you know how to fix this ?
2 Answers
+ 4
To fix the issue of displaying � instead of characters like "Ê" or "è" in your PHP and SQL queries, you can follow these steps:
Database Character Set: Ensure that your database table, column, and connection character set are set to utf8mb4. You mentioned that you have already set your database to utf8mb4_unicode_ci, which is correct. Make sure that the specific table and column where the data is stored also use the utf8mb4 character set.
Connection Encoding: Set the connection encoding in PHP to utf8mb4 before executing any queries. You can do this by executing the following SQL statement immediately after establishing the database connection:
$db_connection->query("SET NAMES 'utf8mb4'");
This ensures that the connection between PHP and the database is using the correct character set.
PHP File Encoding: Ensure that your PHP files are saved with the UTF-8 encoding. You can check this in your code editor's settings or by using a tool like Notepad++ to verify and convert the encoding if needed.
HTML Content-Type: Set the Content-Type meta tag in your HTML file to specify UTF-8 encoding. Place the following line within the <head> section of your HTML file:
<meta charset="UTF-8">
This tells the browser to interpret the HTML document using UTF-8 encoding.
By following these steps, you should be able to resolve the issue and display characters like "Ê" or "è" correctly in your PHP and SQL queries.
Additionally, ensure that the fonts you are using have support for the characters you need. If the fonts don't support those characters, they may still appear incorrectly even after fixing the encoding.
+ 1
I've found !
But it can be different depending on what you are using:
MySQLi: $conn->set_charset("utf8mb4");
PDO (MySQL) :
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4'
);
$conn = new PDO($dsn, $username, $password, $options);
PDO (PostgreSQL) :
$options = array(
PDO::PGSQL_ATTR_INIT_COMMAND => 'SET CLIENT_ENCODING to "UTF8"'
);
$conn = new PDO($dsn, $username, $password, $options);
PDO (SQLite):
SQLite does not require any specific charset configuration.