PHP | mysqli_get_charset() Function

PHP mysqli_get_charset() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_get_charset() Function.

The mysqli_get_charset() function returns an object of the character set class, which contains the following properties below -

  • charset: Name of the character set.
  • collation: Name of the collation.
  • dir: Directory of the character set.
  • min_length: Minimum length of the character set (in bytes).
  • max_length: Maximum length of the character set (in bytes).
  • number: Character set number.
  • state: Character set status.

Syntax

Following below is the syntax to use this function -

mysqli_get_charset($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This PHP function returns an object of the character set class.

PHP Version

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

Example1

The following example below demonstrates the usage of the PHP mysqli_get_charset() function (in a procedural style) -

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

Output

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

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

Example2

In an object oriented style the syntax of this built-in PHP function is $db->get_charset(); Following is the example of this function in an object oriented style $minus;

<?php
   $db = mysqli_init();
   //Connecting to the database
   $db->real_connect("localhost","root","password","test");

   //Name of the character set
   $res = $db->get_charset();

   print_r($res);
?>

Output

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

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

Example3

Try the following example below -

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

Output

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

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"
}

Default character set is: utf8


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_get_client_info() Function.

Do feel free to ask your questions where necessary and we 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.