MySQLi - Definir conjunto de caracteres

Sintaxe

bool mysqli_set_charset ( mysqli $link , string $charset )

Definição e Uso

Ele define o conjunto de caracteres do cliente padrão.

Exemplo

Experimente o seguinte exemplo -

<?php
   $servername = "localhost:3306";
   $username = "root";
   $password = "";
   $dbname = "TUTORIALS";
   $tmp = NULL;
   $conn = new mysqli($servername, $username, $password, $dbname);

   if (!$conn->real_connect($servername, $username, $password, $dbname)) {
      die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
   }
   echo 'Success... ' . mysqli_get_host_info($conn) . "\n";
   printf("character set: %s\n", $conn->character_set_name());

   if (!$conn->set_charset("utf8")) {
      printf("Error loading character set utf8: %s\n", $conn->error);
      exit();
   } else {
      printf("Current character set: %s\n", $conn->character_set_name());
   }
   
   $conn->close();
?>

O exemplo de saída do código acima deve ser assim -

Success... localhost:3306 via TCP/IP character set: latin1 Current character set: utf8