PHP | mysqli_character_set_name() Function

PHP myqli_character_set_name() Function


Hello folks! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_character_set_name() Function.

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

Syntax

Following below is the syntax to use this function -

mysqli_character_set_name($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This built-in PHP function returns a string value representing the name of the current character set.

PHP Version

This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which demonstrates the usage of PHP mysqli_character_set_name() function (in a procedural style) -

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

Output

When the above code is executed, it will produce the following result -

utf8

Example2

In an object oriented style the syntax of this function is $con->character_set_name(); Following is the example of this function in an object oriented style $minus;

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

Output

When the above code is executed, it will produce the following result -

utf8

Example3
Try the following example below -

<?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 "Default character set is: " . $charset;
   
   mysqli_close($connection);
?>

Output
When the above code is executed, it will produce the following result -

Default character set is: utf8


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the PHP mysqli_close() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.