PHP Function Reference

PHP mysqli get_charset() Method



The PHP mysqli::get_charset() / mysqli_get_charset() function returns a character set object providing several properties of the current active character set.

Syntax

//Object-oriented style
public mysqli::get_charset()

//Procedural style
mysqli_get_charset(mysql)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().

Return Value

Returns a character set object with the following properties:

  • charset - Character set name
  • collation - Collation name
  • dir - Directory the charset description was fetched from (?) or "" for built-in character sets
  • min_length - Minimum character length in bytes
  • max_length - Maximum character length in bytes
  • number - Internal character set number
  • state - Character set status (?)

Example: Object-oriented style

The example below shows the usage of mysqli::get_charset() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//displaying character set object
var_dump($mysqli->get_charset());

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

object(stdClass)#2 (7) {
  ["charset"]=>
  string(6) "latin1"
  ["collation"]=>
  string(17) "latin1_swedish_ci"
  ["dir"]=>
  string(0) ""
  ["min_length"]=>
  int(1)
  ["max_length"]=>
  int(1)
  ["number"]=>
  int(8)
  ["state"]=>
  int(801)
}

Example: Procedural style

The example below shows the usage of mysqli_get_charset() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//displaying character set object
var_dump(mysqli_get_charset($mysqli));

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

object(stdClass)#2 (7) {
  ["charset"]=>
  string(6) "latin1"
  ["collation"]=>
  string(17) "latin1_swedish_ci"
  ["dir"]=>
  string(0) ""
  ["min_length"]=>
  int(1)
  ["max_length"]=>
  int(1)
  ["number"]=>
  int(8)
  ["state"]=>
  int(801)
}

❮ PHP MySQLi Reference