What are the potential pitfalls of not setting the database connection to UTF-8 in PHP?

If the database connection is not set to UTF-8 in PHP, it can lead to character encoding issues where special characters may not be displayed correctly or may be corrupted. To solve this issue, you can set the database connection to UTF-8 by running the query "SET NAMES 'utf8'" after establishing the connection.

// Establish database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Set database connection to UTF-8
$conn->query("SET NAMES 'utf8'");