• PHP Video Tutorials

PHP mysqli_get_charset() Function



Definition and Usage

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

  • charset: Name of the character set.

  • collation: Name of the collation.

  • dir: Directory of the character set.

  • min_length: Minimum character length (bytes).

  • max_length: Maximum character length (bytes).

  • number: Character set number.

  • state: Character set status.

Syntax

mysqli_get_charset($con)

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

Return Values

The mysqli_get_charset() function returns an object of the character set class.

PHP Version

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

Example

Following example demonstrates the usage of the mysqli_get_charset() function (in 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);
?>

This will produce 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
)

Example

In object oriented style the syntax of this function is $db->get_charset(); Following is the example of this function in 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);
?>

This will produce 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
)

Example

<?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);
?>

This will produce 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
php_function_reference.htm
Advertisements