Home  >  Article  >  Backend Development  >  How to solve garbled Chinese data added by php insert

How to solve garbled Chinese data added by php insert

PHPz
PHPzOriginal
2023-04-04 14:29:10783browse

When using PHP for database development, we often need to use SQL statements to insert data. However, sometimes when inserting Chinese data into the database, garbled characters will appear. This article will introduce how to solve this problem.

  1. Check the database encoding

First, we need to check the encoding of the database. In MySQL, you can check the encoding method of the database through the following SQL statement:

SHOW VARIABLES LIKE 'character_set_database';

If the output result is utf8 or utf8mb4, the default encoding method of the database is UTF-8. If the output results are in other encodings, you need to specify the corresponding character set when using the INSERT statement.

  1. Set PHP encoding method

In PHP, you can use the following statement to set the encoding method:

mysqli_set_charset($mysqli, 'utf8');

Among them, $mysqli is a connection to The mysqli object of the database. Setting the encoding method to UTF-8 can ensure that garbled characters will not appear when inserting Chinese data into the database.

  1. Specify the character set in the SQL statement

If the above two methods cannot solve the garbled problem, then we need to specify the character set in the INSERT statement. For example:

INSERT INTO `table_name` (`column1`, `column2`, ...) VALUES (N'中文', 'English', ...)

In the SQL statement, use N'' to convert the Chinese string into Unicode encoding to ensure that the Chinese string will not be garbled.

  1. Use prepared statements

Finally, to avoid SQL injection attacks and improve code reusability, we recommend using prepared statements. Prepared statements can be created through mysqli or PDO objects, and then specify the encoding method when setting parameters, for example:

$stmt = $mysqli->prepare("INSERT INTO `table_name` (`column1`, `column2`, ...) VALUES (?, ?, ...)");
$stmt->bind_param("ss", $val1, $val2, ...);
$stmt->execute();

In this example, when setting parameters using the bind_param method, set the character set to 'ss' , which means that the parameter is of string type and the character set is UTF-8.

Summary

This article introduces several methods to solve the problem of adding Chinese garbled characters in PHP INSERT SQL, including checking the database encoding, setting the PHP encoding method, specifying the character set in the SQL statement and using preprocessing statement. During the development process, we should choose the appropriate method according to the actual situation and follow coding standards to ensure the readability and maintainability of the code.

The above is the detailed content of How to solve garbled Chinese data added by php insert. 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