PHP mysqli set_charset() 函数
示例 - 面向对象的样式
设置默认客户端字符集:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
echo "Initial character set is: " . $mysqli -> character_set_name();
// Change character set to utf8
$mysqli -> set_charset("utf8");
echo "Current character set is: " . $mysqli -> character_set_name();
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
echo "Initial character set is: " . $mysqli -> character_set_name();
// Change character set to utf8
$mysqli -> set_charset("utf8");
echo "Current character set is: " . $mysqli -> character_set_name();
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
set_charset() / mysqli_set_charset()
mysqli_set_charset() 函数规定当与数据库服务器进行数据传送时要使用的默认字符集。
注释:在 Windows 平台上使用该函数,您需要 MySQL 客户端库 4.1.11 或以上版本(MySQL 5.0 需要 5.0.6 或以上版本)。
语法
面向对象的风格:
$mysqli ->
set_charset(charset)
程序风格:
mysqli_set_charset(connection, charset)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
charset | 必需。规定默认字符集。 |
技术细节
返回值: | 若成功则返回 TRUE,若失败则返回 FALSE。 |
---|---|
PHP 版本: | 5.0.5+ |
示例 - 程序样式
设置默认客户端字符集:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
echo "Initial character set is: " . mysqli_character_set_name($con);
// Change character set to utf8
mysqli_set_charset($con,"utf8");
echo "Current character set is: " . mysqli_character_set_name($con);
mysqli_close($con);
?>
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
echo "Initial character set is: " . mysqli_character_set_name($con);
// Change character set to utf8
mysqli_set_charset($con,"utf8");
echo "Current character set is: " . mysqli_character_set_name($con);
mysqli_close($con);
?>