PHP Function Reference

PHP mysqli character_set_name() Method



The PHP mysqli::character_set_name() / mysqli_character_set_name() function returns the current character set of the database connection.

Syntax

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

//Procedural style
mysqli_character_set_name(mysql)

Parameters

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

Return Value

Returns the current character set of the connection.

Example: Object-oriented style

The example below shows the usage of mysqli::character_set_name() 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();
}

//setting the default character set
$mysqli->set_charset('utf8mb4');

//printing the current character set
$charset = $mysqli->character_set_name();
printf("Current character set is: %s\n", $charset);

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

The output of the above code will be:

Current character set is: utf8mb4

Example: Procedural style

The example below shows the usage of mysqli_character_set_name() 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();
}

//setting the default character set
mysqli_set_charset($mysqli, 'utf8mb4');

//printing the current character set
$charset = mysqli_character_set_name($mysqli);
printf("Current character set is: %s\n", $charset);

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

The output of the above code will be:

Current character set is: utf8mb4

❮ PHP MySQLi Reference