Home  >  Article  >  Backend Development  >  Tips and practices for handling Chinese garbled characters in databases with PHP

Tips and practices for handling Chinese garbled characters in databases with PHP

WBOY
WBOYOriginal
2024-03-27 17:21:04985browse

Tips and practices for handling Chinese garbled characters in databases with PHP

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples.

Common causes of garbled characters

  1. The database character set is incorrectly set: the correct character set needs to be selected when creating the database, such as utf8 or utf8mb4, otherwise it will cause Chinese garbled characters.
  2. The character set is not specified when PHP connects to the database: The correct character set needs to be set when connecting to the database, otherwise the data returned by the database may be messed up.
  3. The character sets of databases, tables or fields are not uniform: If the character sets of tables or fields in the database are not uniform, garbled characters will appear when inserting or querying data.

Solution

  1. Set the database character set to utf8 or utf8mb4: When creating the database, use the following SQL statement to set the character set:
CREATE DATABASE dbname DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  1. Specify the character set when connecting to the database: When connecting to the database in PHP, you need to use the following code to specify the character set:
$mysqli = new mysqli("localhost", "username", "password", "dbname");
$mysqli->set_charset("utf8");
  1. Set the character set of tables and fields to utf8 or utf8mb4 : When creating a table or field, use the following SQL statement to set the character set:
CREATE TABLE tablename (
    id INT AUTO_INCREMENT,
    content TEXT CHARACTER SET utf8mb4,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Specific code example

Query Chinese data in the database

$mysqli = new mysqli("localhost", "username", "password", "dbname");
$mysqli->set_charset("utf8");

$query = $mysqli->query("SELECT * FROM tablename WHERE id=1");
while ($row = $query->fetch_assoc()) {
    echo $row['content'];
}

Insert Chinese Data to database

$mysqli = new mysqli("localhost", "username", "password", "dbname");
$mysqli->set_charset("utf8");

$content = "中文内容";
$mysqli->query("INSERT INTO tablename (content) VALUES ('" . $mysqli->real_escape_string($content) . "')");

Through the above code examples, we can see that the key to dealing with the problem of Chinese garbled characters in the database in PHP is to correctly set the database character set, specify the character set when connecting, and maintain the character set of tables and fields. consistent. Only in this way can the problem of Chinese garbled characters be effectively avoided and the accuracy and integrity of the data guaranteed.

In summary, I hope this article can help readers solve the problem of Chinese garbled characters in PHP database processing and improve the efficiency and quality of website development. Happy programming everyone!

The above is the detailed content of Tips and practices for handling Chinese garbled characters in databases with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn