MySQL Character Encoding Discrepancies in PHP Installations

When using PHP 5.3.0 with MySQL 5.1.3, different installation methods result in different default character set behaviors when connecting to MySQL without explicitly setting encoding.
Two PHP environments with identical versions but different installation methods:
1. Yum-installed PHP 5.3.0 with MySQL 5.1.3 (my.ini configured with character_set_server=utf8)
2. Manually compiled PHP 5.3.0
Both environments connect to the same MySQL server using mysql_connect() without setting mysql_query("set names utf8") or mysql_set_charset("utf8"). However, they use different default connection character sets.
The following PHP code demonstrates how to check MySQL character variables:
Yum-installed PHP output:
Array
(
    [Variable_name] => character_set_client
    [Value] => latin1
)
Array
(
    [Variable_name] => character_set_connection
    [Value] => latin1
)
Array
(
    [Variable_name] => character_set_database
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_filesystem
    [Value] => binary
)
Array
(
    [Variable_name] => character_set_results
    [Value] => latin1
)
Array
(
    [Variable_name] => character_set_server
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_system
    [Value] => utf8
)
Manually compiled PHP output:
Array
(
    [Variable_name] => character_set_client
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_connection
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_database
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_filesystem
    [Value] => binary
)
Array
(
    [Variable_name] => character_set_results
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_server
    [Value] => utf8
)
Array
(
    [Variable_name] => character_set_system
    [Value] => utf8
)
Examining the compilation parameters through phpinfo() reveals the cause:
For yum-installed PHP:
--with-mysql=shared,/usr --with-mysqli=shared,/usr/lib64/mysql/mysql_config
These extensions use the MySQL Client Library by default. When installed on Linux, this requires the MySQL client package.
For compiled PHP:
div>--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
This uses mysqlnd (MySQL Native Driver).
The key difference is that mysqlnd connects using the server's default character settings as shown in "show global variables like '%char%'".
div>The MySQL client library approach defaults to latin1, effectively maintaining an implicit "set names latin1" setting.

Tags: MySQL PHP Character Encoding Installation Methods mysqlnd

Posted on Wed, 29 Jul 2026 16:16:21 +0000 by don_s