English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP 基础教程

PHP 高级教程

PHP & MySQL

PHP 参考手册

PHP mysqli_get_charset() 函数用法及示例

PHP MySQLi Reference Manual

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中引入的,并且可以在所有更高版本中使用。

Online example

以下示例演示了mysqli_get_charset()函数的用法(面向过程风格)-

<?php
  $db = mysqli_init();
  //建立连接
  mysqli_real_connect($db, "localhost","root","password","test");
  //字符集
  $res = mysqli_get_charset($db);
  print_r($res);
?>

Output result

stdClass Object
(
    [charset] => utf8
    [collation] => utf8_general_ci
    [dir] =>
    [min_length] => 1
    [max_length] => 3
    [number] => 33
    [state] => 1
    [comment] => UTF-8 Unicode
)

Online example

In object-oriented style, the syntax of this function is$db->get_charset();.Here is an example of this function in object-oriented style:

<?php
   $db = mysqli_init();
   //Connect to database
   $db->real_connect("localhost","root","password","test");
   //Charset name
   $res = $db->get_charset();
   print_r($res);
?>

Output result

stdClass Object
(
    [charset] => utf8
    [collation] => utf8_general_ci
    [dir] =>
    [min_length] => 1
    [max_length] => 3
    [number] => 33
    [state] => 1
    [comment] => UTF-8 Unicode
)

Online example

Returns a charset object with properties and the default charset:

<?php
   $connection_mysql = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Connection to MySQL failed: " . mysqli_connect_error();
   }
   
   var_dump(mysqli_get_charset($connection_mysql));
   mysqli_close($connection_mysql);
?>

Output result

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

PHP MySQLi Reference Manual