English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A função mysqli_set_charset() define o código de caracteres padrão
mysqli_set_charset()a função é usada para especificar o conjunto de caracteres padrão, o conjunto de caracteres padrão para enviar dados ao servidor do banco de dados a partir do cliente mysqli.
Atenção: ao usar essa função no Windows, você precisa da biblioteca do cliente MySQL 4.1.11 ou versão superior (MySQL 5.0 necessário 5.0.6 or higher version).
mysqli_set_charset($con, charset)
Serial number | Parameters and descriptions |
---|---|
1 | con (required) This is an object representing the connection to the MySQL Server. |
2 | charset (required) The name of the character set to be set as the default. |
mysqli_set_charset()Returns TRUE on success, or FALSE on failure.
This function was originally in PHP version5introduced and can be used in all higher versions.
The following examples demonstratemysqli_set_charset()Usage of the function (procedural style)-
<?php //Establish connection $con = mysqli_connect("localhost", "root", "password", "mydb"); //Character set name $res = mysqli_set_charset($con, "utf8"); print_r($res); //Close connection mysqli_close($con); ?>
Output result
1
In object-oriented style, the syntax of this function is$con-> set_charset();.Here is an example of this function in object-oriented style;
<?php $con = new mysqli("localhost", "root", "password", "test"); //Character set name $res = $con-> set_charset("utf8"); print($res); //Close connection $con -> close(); ?>
Output result
1
Set default client character set:
<?php $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "Connection to MySQL failed: " . mysqli_connect_error(); } mysqli_set_charset($connection_mysql, "utf8"); echo mysqli_character_set_name($connection_mysql); mysqli_close($connection_mysql); ?>
Output result
utf8