
mysqli_get_charset()函数返回字符集对象
定义和用法
mysqli_get_charset()函数返回字符集类的对象,其中包含以下属性:
- charset: 字符集的名称。 
- collation: 排序规则的名称。 
- dir: 被获取的目录字符集或者 ""。 
- min_length: 最小字符长度(字节)。 
- max_length: 最大字符长度(字节)。 
- number: 内部字符集数。 
- state: 字符集状态。 
语法
mysqli_get_charset($con)
参数
| 序号 | 参数及说明 | 
|---|---|
| 1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 | 
返回值
mysqli_get_charset()函数返回的字符集的类的对象。
PHP版本
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
在线示例
以下示例演示了mysqli_get_charset()函数的用法(面向过程风格)-
<?php $db = mysqli_init(); //建立连接 mysqli_real_connect($db, "localhost","root","password","test"); //字符集 $res = mysqli_get_charset($db); print_r($res); ?>
输出结果
stdClass Object ( [charset] => utf8 [collation] => utf8_general_ci [dir] => [min_length] => 1 [max_length] => 3 [number] => 33 [state] => 1 [comment] => UTF-8 Unicode )
在线示例
在面向对象的样式中,此函数的语法为$db->get_charset();。以下是面向对象样式中此函数的示例;
<?php
   $db = mysqli_init();
   //连接到数据库
   $db->real_connect("localhost","root","password","test");
   //字符集名称
   $res = $db->get_charset();
   print_r($res);
?>输出结果
stdClass Object ( [charset] => utf8 [collation] => utf8_general_ci [dir] => [min_length] => 1 [max_length] => 3 [number] => 33 [state] => 1 [comment] => UTF-8 Unicode )
在线示例
返回带有属性的字符集对象以及默认的字符集:
<?php
   $connection_mysql = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "连接MySQL失败: " . mysqli_connect_error();
   }
   
   var_dump(mysqli_get_charset($connection_mysql));
   mysqli_close($connection_mysql);
?>输出结果
object(stdClass)#2 (8) {
  ["charset"]=>
  string(4) "utf8"
  ["collation"]=>
  string(15) "utf8_general_ci"
  ["dir"]=>
  string(0) ""
  ["min_length"]=>
  int(1)
  ["max_length"]=>
  int(3)
  ["number"]=>
  int(33)
  ["state"]=>
  int(1)
  ["comment"]=>
  string(13) "UTF-8 Unicode"
}
Default character set is: utf8
                    
                