self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName.";"." charset=utf8", self::$dbUsername, self::$dbUserPassword);
(PHP 5 >= 5.0.5, PHP 7)
mysqli::set_charset -- mysqli_set_charset — 设置默认字符编码
面向对象风格
$charset
)过程化风格
设置在数据库间传输字符时所用的默认字符编码。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Note:
如果在Windows平台上使用该方法,需要4.1.11版或以上的MySQL客户端库,且MySQL版本为5.0.6以上。
Note:
这应该是首选的用于改变字符编码的方法,不建议使用mysqli_query()执行SQL请求的SET NAMES ...(如 SET NAMES utf8)。 详见MySQL字符集的概念
Example #1 mysqli::set_charset() example
面向对象风格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
$mysqli->close();
?>
过程化风格
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!mysqli_set_charset($link, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($link));
} else {
printf("Current character set: %s\n", mysqli_character_set_name($link));
}
mysqli_close($link);
?>
以上例程会输出:
Current character set: utf8
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName.";"." charset=utf8", self::$dbUsername, self::$dbUserPassword);
The comment by Claude (http://php.net/manual/en/mysqli.set-charset.php#121067) is CORRECT.
Setting the charset (it's really the encoding) like this after setting up your connection:
$connection->set_charset("utf8mb4")
FAILS to set the proper collation for the connection:
character_set_client: utf8mb4
character_set_connection: utf8mb4
character_set_database: utf8mb4
character_set_filesystem: binary
character_set_results: utf8mb4
character_set_server: utf8mb4
character_set_system: utf8
collation_connection: utf8mb4_general_ci <---- still says general
collation_database: utf8mb4_unicode_ci
collation_server: utf8mb4_unicode_ci
If you use SET NAMES, that works:
$connection->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
character_set_client: utf8mb4
character_set_connection: utf8mb4
character_set_database: utf8mb4
character_set_filesystem: binary
character_set_results: utf8mb4
character_set_server: utf8mb4
character_set_system: utf8
collation_connection: utf8mb4_unicode_ci <-- now says unicode
collation_database: utf8mb4_unicode_ci
collation_server: utf8mb4_unicode_ci
Please note, that I set the following variables on the server:
Set the following to be: utf8mb4_unicode_ci
character_set_client
character_set_connection
character_set_database
character_set_results
character_set_server
collation_connection
collation_server
Set:
character-set-client-handshake = FALSE or 0
skip-character-set-client-handshake = TRUE or 1
Although the documentation says that using that function is preferred than using SET NAMES, it is not sufficient in case you use a collation different from the default one:
<?php
// That will reset collation_connection to latin1_swedish_ci
// (the default collation for latin1):
$mysqli->set_charset('latin1');
// You have to execute the following statement *after* mysqli::set_charset()
// in order to get the desired value for collation_connection:
$mysqli->query("SET NAMES latin1 COLLATE latin1_german1_ci");
So in my case, I had tried changing the collation from utf8mb4_unicode_ci for mysql and had to change it to uft8_general_ci.
Then pasted :
mysqli_set_charset( $con, 'utf8');
right before I did the SELECT command.
This is my code for reading from db :
/*
$DB_SERVER="db_server_name";
$DB_USER_READER="root";
$DB_PASS_READER="passw*rd";
$DB_NAME="db_name";
$DB_PORT="port number";
$SELECT_WHAT="`name_of_column_as_in_your_table`";
$WHICH_TBL="`table_name`";
$ON_WHAT_CONDITION="`id`='7'";
*/
$con = mysqli_connect($DB_SERVER, $DB_USER_READER, $DB_PASS_READER, $DB_NAME, $DB_PORT);//this is the unique connection for the selection
mysqli_set_charset( $con, 'utf8');
$slct_stmnt = "SELECT ".$SELECT_WHAT." FROM ".$WHICH_TBL." WHERE ".$ON_WHAT_CONDITION;
$slct_query = mysqli_query($con, $slct_stmnt);
if ($slct_query==true) {
//Do your stuff here . . .
}
And it worked like a charm. All the best. The above code can work with reading chineese, russian or arabic or any international language from the database's table column holding such data.
On windows, with "stock" PHP 5.1.2, there is no set_charset function at all. One may have to replace php_mysqli.dll, and libmysql.dll with proper versions, which provided by MySQL at http://dev.mysql.com/downloads/connector/php/