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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_get_charset() Function Usage and Example

PHP MySQLi Reference Manual

mysqli_get_charset() function returns a character set object

Definition and usage

mysqli_get_charset()The function returns an object of the character set class, which includes the following properties:

  • charset:  The name of the character set.

  • collation: The name of the sorting rule.

  • dir: The directory character set obtained or "".

  • min_length: Minimum character length (bytes).

  • max_length: Maximum character length (bytes).

  • number: Internal character set number.

  • state: Character set status.

Syntax

mysqli_get_charset($con)

Parameter

Serial numberParameters and descriptions
1

con(Required)

This is an object representing the connection with the MySQL Server.

Return value

mysqli_get_charset()The class object of the character set returned by the function.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online example

The following examples demonstratemysqli_get_charset()Function usage (procedural style)-

<?php
  $db = mysqli_init();
  //Establishing connection
  mysqli_real_connect($db, "localhost","root","password","test");
  //Character set
  $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 attributes and the default character set:

<?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