PHP mysqli::character_set_name() / mysqli_character_set_name() 函数返回数据库连接的当前字符集。
语法
//面向对象风格
public mysqli::character_set_name()
//面向过程风格
mysqli_character_set_name(mysql)
参数
mysql | 必需。 仅适用于面向过程风格:指定 mysqli_connect() 或 mysqli_init() 返回的 mysqli 对象。 |
返回值
返回连接的当前字符集。
示例:面向对象风格
下面的示例显示mysqli::character_set_name()的用法
<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: ". $mysqli->connect_error;
exit();
}
//设置默认字符集
$mysqli->set_charset('utf8mb4');
//打印当前字符集
$charset = $mysqli->character_set_name();
printf("Current character set is: %s\n", $charset);
//关闭连接
$mysqli->close();
?>
上述代码的输出将是:
Current character set is: utf8mb4
示例:面向过程风格
下面的示例显示了mysqli_character_set_name() 函数的用法。
<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ". mysqli_connect_error();
exit();
}
//设置默认字符集
mysqli_set_charset($mysqli, 'utf8mb4');
//打印当前字符集
$charset = mysqli_character_set_name($mysqli);
printf("Current character set is: %s\n", $charset);
//关闭连接
mysqli_close($mysqli);
?>
上述代码的输出将是:
Current character set is: utf8mb4