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

PHP: How to generate a random, unique, alphanumeric string for use in a secret link?

Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

If you do not need it to be absolutely unique over time:

md5(uniqid(rand(), true))

Otherwise (given you have already determined a unique login for your user):

md5(uniqid($your_user_login, true))

Ref : https://bit.ly/31fd9wa

 





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