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 Manuale di riferimento

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 contains 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(必需)

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 version 5 and can be used in all higher versions.

Esempio online

The following examples demonstratemysqli_get_charset()Usage of the function (procedural style)-

<?php
  $db = mysqli_init();
  //Establish connection
  mysqli_real_connect($db, "localhost","root","password","test");
  //Character set
  $res = mysqli_get_charset($db);
  print_r($res);
?>

Risultato di output

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

Esempio online

In the object-oriented style, the syntax of this function is$db->get_charset();。Ecco un esempio di come funziona questa funzione in uno stile orientato agli oggetti;

<?php
   $db = mysqli_init();
   //Connettersi al database
   $db->real_connect("localhost","root","password","test");
   //Nome del set di caratteri
   $res = $db->get_charset();
   print_r($res);
?>

Risultato di output

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

Esempio online

Restituisce un oggetto charset con attributi e il set di caratteri predefinito:

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

Risultato di output

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"
}
Il set di caratteri predefinito è: utf8

PHP MySQLi Manuale di riferimento