<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/mysqlinfo.concepts.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'mysqlinfo.concepts.charset.php',
    1 => 'Character sets',
    2 => 'Character sets',
  ),
  'up' => 
  array (
    0 => 'mysqlinfo.concepts.php',
    1 => 'Concepts',
  ),
  'prev' => 
  array (
    0 => 'mysqlinfo.concepts.buffering.php',
    1 => 'Buffered and Unbuffered queries',
  ),
  'next' => 
  array (
    0 => 'book.mysqli.php',
    1 => 'MySQLi',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysqlinfo/concepts.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mysqlinfo.concepts.charset" class="section">
  <h2 class="title">Character sets</h2>

  <p class="para">
   Ideally a proper character set will be set at the server level, and doing this is described
   within the <a href="http://dev.mysql.com/doc/mysql/en/charset-configuration.html" class="link external">&raquo;&nbsp;Character Set Configuration</a>
   section of the MySQL Server manual. Alternatively, each MySQL API offers a method to set 
   the character set at runtime.
  </p>

  <div class="caution"><strong class="caution">Застереження</strong>
   <h1 class="title">The character set and character escaping</h1>
   <p class="para">
    The character set should be understood and defined, as it has an affect on every
    action, and includes security implications. For example, the escaping mechanism
    (e.g., <span class="function"><a href="mysqli.real-escape-string.php" class="function">mysqli_real_escape_string()</a></span> for mysqli and <span class="methodname"><a href="pdo.quote.php" class="methodname">PDO::quote()</a></span> for PDO_MySQL) will adhere to
    this setting. It is important to realize that these functions will not use the character
    set that is defined with a query, so for example the following will not have an effect
    on them:
   </p>
  </div>

  <div class="example" id="example-1">
   <p><strong>Приклад #1 Problems with setting the character set with SQL</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Will NOT affect $mysqli-&gt;real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SET NAMES utf8mb4"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Will NOT affect $mysqli-&gt;real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SET CHARACTER SET utf8mb4"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// But, this will affect $mysqli-&gt;real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'utf8mb4'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// But, this will NOT affect it (UTF-8 vs utf8mb4) -- don't use dashes here<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Below are examples that demonstrate how to properly alter the character set at runtime
   using each API.
  </p>
  
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <strong>Possible UTF-8 confusion</strong><br />
   <p class="para">
    Because character set names in MySQL do not contain dashes, the string 
    &quot;utf8&quot; is valid in MySQL to set the character set to UTF-8 (up to 3 byte UTF-8 Unicode Encoding). The string 
    &quot;UTF-8&quot; is not valid, as using &quot;UTF-8&quot; will fail to change the character set and will throw an error.
   </p>
  </p></blockquote>

  <div class="example" id="example-2">
   <p><strong>Приклад #2 Setting the character set example: mysqli</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">'Initial character set: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">character_set_name</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />if (!</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'utf8mb4'</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error loading character set utf8mb4: %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">error</span><span style="color: #007700">);<br />    exit;<br />}<br /><br />echo </span><span style="color: #DD0000">'Your current character set is: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">character_set_name</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-3">
   <p><strong>Приклад #3 Setting the character set example: <a href="ref.pdo-mysql.connection.php" class="link">pdo_mysql</a></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$pdo </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysql:host=localhost;dbname=world;charset=utf8mb4"</span><span style="color: #007700">, </span><span style="color: #DD0000">'my_user'</span><span style="color: #007700">, </span><span style="color: #DD0000">'my_pass'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

 </div><?php manual_footer($setup); ?>