<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'mysqlinfo.concepts.charset.php',
    1 => 'Jeux de caract&egrave;res',
    2 => 'Jeux de caract&egrave;res',
  ),
  'up' => 
  array (
    0 => 'mysqlinfo.concepts.php',
    1 => 'Concepts',
  ),
  'prev' => 
  array (
    0 => 'mysqlinfo.concepts.buffering.php',
    1 => 'Requ&ecirc;tes mises ou non en m&eacute;moire tampon',
  ),
  'next' => 
  array (
    0 => 'book.mysqli.php',
    1 => 'MySQLi',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Jeux de caractères</h2>

  <p class="simpara">
   Idéalement, un jeu de caractères propre doit être défini
   au niveau du serveur, opération décrite dans la section
   <a href="http://dev.mysql.com/doc/mysql/en/charset-configuration.html" class="link external">&raquo;&nbsp;Configuration du jeu de
    caractères</a> du manuel MySQL Server. Alternativement, chaque API
   MySQL offre une méthode pour définir le jeu de caractères lors de
   l&#039;exécution.
  </p>

  <div class="caution"><strong class="caution">Attention</strong>
   <h1 class="title"> Le jeu de caractères et l&#039;échappement des caractères</h1>
   <p class="simpara">
    Le jeu de caractères doit être compris et défini, sachant qu&#039;il a un
    effet sur chaque action, et a des implications au niveau de la
    sécurité. Par exemple, le mécanisme d&#039;échappement (c.-à-d.
    <span class="function"><a href="mysqli.real-escape-string.php" class="function">mysqli_real_escape_string()</a></span> pour mysqli, et
    <span class="methodname"><a href="pdo.quote.php" class="methodname">PDO::quote()</a></span> pour PDO_MySQL) va utiliser
    cette configuration. Il est important de garder en tête que ces fonctions
    n&#039;utiliseront pas le jeu de caractères défini avec une requête, aussi,
    l&#039;exemple suivant n&#039;aura aucun effet sur le jeu de caractères :
   </p>
  </div>

  <div class="example" id="example-1">
   <p><strong>Exemple #1 Problèmes lors de la définition du jeu de caractères avec 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">// N'affectera PAS $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">// N'affectera PAS $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">// Mais, ceci affectera $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">// Mais, ceci NE l'affectera PAS (UTF-8 par rapport à utf8mb4) -- n'utiliser pas les tirets ici<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="simpara">
   Ci-dessous les exemples qui démontrent la façon de modifier proprement
   le jeu de caractères lors de l&#039;exécution en utilisant chacune des APIs.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Confusion possible avec UTF-8</strong><br />
   <span class="simpara">
    Dû au fait que les noms des jeux de caractères dans MySQL ne contiennent pas de tiret,
    la chaîne &quot;utf8&quot; est correcte dans MySQL et définira le jeu de caractères en UTF-8.
    La chaîne &quot;UTF-8&quot; n&#039;est pas correcte, et utiliser &quot;utf-8&quot; échouera à modifier le
    jeu de caractères.
   </span>
  </p></blockquote>

  <div class="example" id="example-2">
   <p><strong>Exemple #2 Exemple de définition du jeu de caractères : 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">'Jeu de caractère initial : ' </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">"Erreur lors du chargement du jeu de caractères 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">'Votre jeu de caractère actuel est : ' </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>Exemple #3 Exemple de définition du jeu de caractères : <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); ?>