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

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP mysqli_character_set_name() function

PHP MySQLi Reference Manual

The mysqli_character_set_name() function returns the default character encoding of the current database connection

Definition and usage

mysqli_character_set_name()The function returns the name of the default character set of the current database connection.

Syntax

mysqli_character_set_name($con)

Parameter

Serial numberParameters and descriptions
1

con(Required)

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

Return value

mysqli_character_set_name()The function returns a string value representing the current character set.

PHP version

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

Online example

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

//Establish connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Name of the character set
$res = mysqli_character_set_name($con);
print($res);
//Close connection
mysqli_close($con);
?>

Output result

utf8

Online example

In the object-oriented style, the syntax for this function is$con->character_set_name();.The following is an example of using this function in an object-oriented style;

<?php
   $con = new mysqli("localhost", "root", "password", "test");
   //Name of the character set
   $res = $con->character_set_name();
   print($res);
   //Close connection
   $con -> close();
?>

Output result

utf8

Online example

Use mysqli_character_set_name() to get the default character set name

<?php
  $connection = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   $charset = mysqli_character_set_name($connection);
   echo "The default character set is: " . $charset;
   
   mysqli_close($connection);
?>

Output result

The default character set is: utf8

PHP MySQLi Reference Manual