w3resource

PHP mysqli: set_charset() function

mysqli_set_charset() function / mysqli::set_charset

The mysqli_set_charset() function / mysqli::set_charset specifies the default character set to be used when sending data from and to the database server.

Note: For this function to work on a Windows platform, you need MySQL client library 4.1.11 or above (for MySQL 5.0 you need 5.0.6 or above).

Syntax:

Object oriented style

bool mysqli::set_charset ( string $charset )

Procedural style

bool mysqli_set_charset ( mysqli $link , string $charset )

Parameter:

Name Description Required/Optional
link A link identifier returned by mysqli_connect() or mysqli_init() Required for procedural style only and Optional for Object oriented style
charset The charset to be set as default. Required

Usage: Procedural style

mysqli_set_charset(connection,charset);

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use Required
charset Specifies the default character set Required

Return value:

Returns TRUE on success or FALSE on failure.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

printf("Initial character set: %s\n", $mysqli->character_set_name());

/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit();
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

$mysqli->close();
?>

Output:

Initial character set: latin1 Current character set: utf8

Example of the Procedural style

<?php
$link = mysqli_connect('localhost', 'user1', 'datasoft123', 'hr');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

printf("Initial character set: %s\n", mysqli_character_set_name($link));

/* change character set to utf8 */
if (!mysqli_set_charset($link, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($link));
    exit();
} else {
    printf("Current character set: %s\n", mysqli_character_set_name($link));
}

mysqli_close($link);
?>

Output:

Initial character set: latin1
Current character set: utf8

Example:

<?php
$con=mysqli_connect("localhost","user1","datasoft123","hr");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

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

mysqli_close($con);
?>

Sample Output:

Initial character set: latin1 Current character set: utf8

See also

PHP Function Reference

Previous: select_db
Next: set_local_infile_default



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

To use the constant simply use its name:

Example:

if (EARTH_IS_FLAT) {
 print "Earth is flat";
}
print APP_ENV_UPPERCASE; 

Output:


or if you don't know the name of the constant in advance, use the constant function:

// this code is equivalent to the above code
$const1 = "EARTH_IS_FLAT";
$const2 = "APP_ENV_UPPERCASE";
if (constant($const1)) {
 print "Earth is flat";
}
print constant($const2);

Output:


 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook