Home  >  Article  >  Backend Development  >  How to solve the problem of garbled characters inserted into the database in PHP

How to solve the problem of garbled characters inserted into the database in PHP

PHPz
PHPzOriginal
2023-03-28 15:00:29733browse

When using PHP to insert data into the database, sometimes you will encounter garbled characters. This is because character sets are inconsistent during data transmission. This article will introduce how to solve the problem of garbled characters inserted into the database by PHP.

1. Check the database character set

Before starting to solve the problem, you must first check the database character set. You can use the following command to view the character set of the current database:

SHOW VARIABLES LIKE 'character_set_database';

If the database character set is not UTF-8, then you need to modify it to UTF-8 to ensure that the data can be inserted and read correctly. You can use the following command to modify the database character set to UTF-8:

ALTER DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

2. Set the PHP file encoding format

Set the encoding format at the top of the PHP file to UTF- 8. This ensures that the strings in PHP can be correctly inserted into the database. You can use the following code to set the PHP encoding format:

header('Content-Type: text/html; charset=UTF-8');

3. Set the mysqli encoding format

When using the mysqli extension to insert data in PHP, you need to set the mysqli encoding format. You can use the following code to set the mysqli encoding format:

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

4. Use the htmlspecialchars function

Before inserting data, use the PHP built-in function htmlspecialchars to convert the string that needs to be inserted. escape. This prevents special characters from affecting the database. You can use the following code to escape the string:

$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');

5. Use prepared statement

Using prepared statement can effectively prevent SQL injection and avoid garbled characters. question. You can use the following code to insert data using prepared statement:

$stmt = $mysqli -> prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?)");
$stmt -> bind_param("ss", $val1, $val2);
$val1 = htmlspecialchars($val1, ENT_QUOTES, 'UTF-8');
$val2 = htmlspecialchars($val2, ENT_QUOTES, 'UTF-8');
$stmt -> execute();

6. Use PDO

Using PDO can handle database operations more conveniently and effectively avoid garbled code problems. . You can use the following code to insert data using PDO:

$conn = new PDO("mysql:host=localhost;dbname=database_name;charset=utf8", "username", "password");
$stmt = $conn -> prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?)");
$stmt -> execute([$val1, $val2]);

7. Summary

When using PHP to insert into the database, it is very common to encounter garbled code problems. However, you can easily solve the problem of garbled characters by simply following the above steps. It should be noted that before performing database operations, you must first check whether the database character set is correct. This is an important step to avoid garbled characters.

The above is the detailed content of How to solve the problem of garbled characters inserted into the database in 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