UTF-8 (Unicode Transformation Format-8) is a variable-length character encoding capable of representing all characters in the Unicode character set. It stores English characters in 1 byte and other characters in 2 to 4 bytes. Its advantages include unified representation of global languages and symbols, compatibility with ASCII, and widespread use in web development and database storage.
In PHP, UTF-8 encoding is commonly used for handling data exchange and storage in multilingual websites, especially when transmitting data between databases and browsers. Incorrect encoding settings can easily lead to garbled characters.
In PHP, if you are using a MySQL database and the mysqli extension to connect and interact with the database, it is crucial to ensure the connection uses UTF-8 encoding. Otherwise, even if your database tables and columns are set to UTF-8, data transfer may still result in garbled characters due to encoding mismatches.
The mysqli::set_charset method allows you to set the character set for the current database connection. By setting it to UTF-8, you ensure that PHP and MySQL correctly encode and decode data during queries and inserts.
Create a Database Connection:
Use mysqli_connect or new mysqli() to connect to the database.
Set the Character Set:
After successfully connecting to the database, use mysqli::set_charset to set the character set to UTF-8.
<?php
// 1. Create a database connection
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'your_database';
<p>$conn = new mysqli($host, $username, $password, $dbname);</p>
<p>// Check connection<br>
if ($conn->connect_error) {<br>
die("Connection failed: " . $conn->connect_error);<br>
}</p>
<p>// 2. Set the character set to UTF-8<br>
if (!$conn->set_charset("utf8")) {<br>
echo "Error: Unable to set UTF-8 character set: " . $conn->error;<br>
} else {<br>
echo "Current character set: " . $conn->character_set_name();<br>
}</p>
<p>// 3. Execute queries<br>
$sql = "SELECT * FROM your_table";<br>
$result = $conn->query($sql);</p>
<p>if ($result->num_rows > 0) {<br>
while($row = $result->fetch_assoc()) {<br>
echo "Data: " . $row['column_name'] . "<br>";<br>
}<br>
} else {<br>
echo "No data";<br>
}</p>
<p>// 4. Close the connection<br>
$conn->close();<br>
?>
Database Connection:
Create a database connection using new mysqli() and check if the connection is successful. If the connection fails, die() terminates the program and displays the error message.
Set Character Set:
Use $conn->set_charset("utf8") to set the connection character set to UTF-8. All data transferred between PHP and the MySQL database will now use UTF-8 encoding. If the setting fails, $conn->error outputs the error message.
Execute Queries:
Query data from the your_table table, ensuring that the returned data correctly displays UTF-8 encoded characters.
Close Connection:
Finally, close the database connection using $conn->close().
Database Table Character Set:
In addition to setting the character set in PHP, ensure that your database tables and columns also use UTF-8. You can check a table's character set with SHOW CREATE TABLE or modify it using the following SQL statement:
ALTER TABLE your_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Browser Encoding:
Ensure that the correct character set is set in the HTML page’s tag to prevent garbled characters in the browser. You can use the following tag:
<meta charset="UTF-8">
MySQL Server Default Character Set:
To make the MySQL server use UTF-8 by default, configure it in the MySQL configuration file (my.cnf or my.ini):
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
Using mysqli::set_charset to set UTF-8 encoding is an essential step to ensure proper handling of multilingual data between PHP and MySQL. Correct character set configuration prevents garbled characters and ensures that data is not lost or misencoded during transfer. Make sure the database tables and columns also use UTF-8 to maintain consistent encoding across your application, improving compatibility and scalability.
Related Tags:
mysqli