Home  >  Article  >  Backend Development  >  How to solve the problem of Chinese garbled characters when inserting data into the database in PHP

How to solve the problem of Chinese garbled characters when inserting data into the database in PHP

PHPz
PHPzOriginal
2023-04-10 09:44:46581browse

PHP is a popular open source scripting language mainly used for web development. When we use PHP for development, we may encounter the problem of Chinese garbled characters, especially when inserting Chinese data into the database. This article will introduce how to avoid this situation in PHP.

  1. Set character encoding

When we connect to the database, we need to set the database character set to be consistent with the character set of the PHP script, otherwise garbled characters will appear when inserting Chinese information. We add the following code when connecting to the database:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_query($con,"SET NAMES utf8");
  1. Insist on using UTF-8 encoding

UTF-8 is a multi-byte encoding that supports the Unicode character set. Using UTF-8 encoding can ensure that data containing Chinese in the database can be inserted correctly. We can check whether the PHP file is UTF-8 encoded by the following method:

$encoding = mb_detect_encoding($str, "UTF-8,GBK,GB2312,BIG5");

If the returned $encoding is "UTF-8", the PHP file is UTF-8 encoded.

  1. Use PHP functions to escape strings

When executing SQL statements, we need to use functions to convert strings into SQL-safe strings to ensure the data in the database safety. PHP provides a function to escape strings - mysqli_real_escape_string(). We can just escape the string before inserting new data.

$name=mysqli_real_escape_string($con,$_POST['name']);
$sql="INSERT INTO TABLE_NAME(name) VALUES('$name')";
mysqli_query($con,$sql);
  1. Using PHP Data Objects (PDO)

PDO is a PHP database abstraction layer that can support a variety of databases. When using PDO, we can set the character encoding for the database connection and use the PDO::quote() function to escape.

$dsn = "mysql:host=localhost;dbname=myDatabase;charset=utf8";
$username = "username";
$password = "password";
$options = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
);

try {
    $pdo = new PDO($dsn, $username, $password, $options);
    $name = $pdo->quote($_POST['name']);
    $sql = "INSERT INTO TABLE_NAME(name) VALUES($name)";
    $pdo->exec($sql);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
    die();
}

When using php insert, if you encounter Chinese garbled characters, we can solve it through the above method. Correct character encoding settings, string escaping, and PDO use can ensure that we can successfully insert Chinese data into the database at any time.

The above is the detailed content of How to solve the problem of Chinese garbled characters when inserting data 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