wpdb::set_charset( mysqli $dbh, string $charset = null, string $collate = null )

In this article

Sets the connection’s character set.

Parameters

$dbhmysqlirequired
The connection returned by mysqli_connect().
$charsetstringoptional
The character set.

Default:null

$collatestringoptional
The collation.

Default:null

Source

public function set_charset( $dbh, $charset = null, $collate = null ) {
	if ( ! isset( $charset ) ) {
		$charset = $this->charset;
	}
	if ( ! isset( $collate ) ) {
		$collate = $this->collate;
	}
	if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
		$set_charset_succeeded = true;

		if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
			$set_charset_succeeded = mysqli_set_charset( $dbh, $charset );
		}

		if ( $set_charset_succeeded ) {
			$query = $this->prepare( 'SET NAMES %s', $charset );
			if ( ! empty( $collate ) ) {
				$query .= $this->prepare( ' COLLATE %s', $collate );
			}
			mysqli_query( $dbh, $query );
		}
	}
}

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.