Current Location: Home> Function Categories> mysqli::set_charset

mysqli::set_charset

(mysqli_set_charset) Set the default client character set
Name:mysqli::set_charset
Category:MySQLi
Programming Language:php
One-line Description:Sets the default client character set.

Definition and usage

set_charset() / mysqli_set_charset() function specifies the default character set used when sending and receiving data with the database server.

Note: To use this function on Windows platforms, you need MySQL Client Library 4.1.11 or later (for MySQL 5.0, you need 5.0.6 or later).

Example

Example 1 - Object-Oriented Style

Change the default client character set:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

echo "Initial character set is: " . $mysqli -> character_set_name ( ) ;

// Change the character set to utf8
$mysqli -> set_charset ( "utf8" ) ;

echo "Current character set is: " . $mysqli -> character_set_name ( ) ;

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Change the default client character set:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

// Check the connection
if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ;
}

echo "Initial character set is: " . mysqli_character_set_name ( $con ) ;

// Change the character set to utf8
mysqli_set_charset ( $con , "utf8" ) ;

echo "Current character set is: " . mysqli_character_set_name ( $con ) ;

mysqli_close ( $con ) ;
?>
Similar Functions