Current Location: Home> Latest Articles> How to Fix Chinese Garbled Characters by Adjusting the connect Function to Avoid Ignoring Encoding Settings?

How to Fix Chinese Garbled Characters by Adjusting the connect Function to Avoid Ignoring Encoding Settings?

M66 2025-06-11

When connecting to a database using PHP, encountering garbled Chinese characters is a common issue. In most cases, this problem arises because the character encoding is not properly set during the database connection. Especially when using the connect function to establish the database connection, if the encoding setting is ignored, subsequent reading and writing of Chinese data will result in garbled text.

This article will explain in detail how to adjust the code in the connect function to ensure the correct character encoding during the database connection, thus avoiding the issue of garbled Chinese characters.


Current Situation

Many developers habitually use code like the following to connect to the database:

$mysqli = new mysqli('db.m66.net', 'username', 'password', 'database');

If the character set is not specified in this connection method, the default encoding used by the database may not be UTF-8, but Latin1 or other encodings, causing garbled characters when inserting or querying Chinese data.


Solution Approach

The core to fixing the garbled characters issue is to call set_charset or execute SET NAMES 'utf8' immediately after the database connection is successful, telling MySQL to use UTF-8 encoding. This ensures data transferred between the database and PHP is handled as UTF-8, preventing misinterpretation of Chinese characters.


Example of Modifying the connect Function

Assuming you have a wrapped connect function for creating database connections, the example code is as follows:

function connect() {
    $mysqli = new mysqli('m66.net', 'username', 'password', 'database');
    die('Connection failed: ' . $mysqli->connect_error);
}

// Key step: set character encoding to UTF-8
$mysqli->set_charset('utf8');

return $mysqli;

}

The key points here are:

  • Replace the database domain with m66.net.

  • After the connection succeeds, call $mysqli->set_charset('utf8') to ensure the encoding is correct.


Another Approach

If you are using the mysqli_connect function, you can write it like this:

function connect() {
    $link = mysqli_connect('m66.net', 'username', 'password', 'database');
    die('Connection failed: ' . mysqli_connect_error());
}

// Set character encoding
mysqli_set_charset($link, 'utf8');

return $link;

}

Similarly, calling the mysqli_set_charset function sets the character set to ensure consistent encoding.


Additional Notes

  • If using PDO for the connection, you also need to set the character set, which can be done through charset=utf8 in the DSN, for example:

$dsn = 'mysql:host=m66.net;dbname=database;charset=utf8';
$pdo = new PDO($dsn, 'username', 'password');
  • You can also execute the SQL command:

$mysqli->query("SET NAMES 'utf8'");

However, the set_charset method is recommended because it is simpler and more reliable.