title: mysqli not setting charset to utf8mb4
body: <p><strong>Problem found</strong></p>
  
  <p>The problem seems to be that <code>$mysqli-&gt;set_charset()</code> is not accepting `utf8mb4' as a valid encoding (just as I "speculated" in the first update). MySQL version is 5.5.41 and PHP version is 5.4.41 (no problem with that).</p>
  
  <hr>
  
  <p>Sorry for the title, I've been searching/reading about what/where can the problem be and I'm already too confused about this...</p>
  
  <p>I recently started using utf8mb4 in mysql. I'm using utf8mb4 as charset and utf8mb4_unicode_ci as collation for all tables/columns.</p>
  
  <p>So I first I changed:</p>
  
  <pre><code>$mysqli-&gt;set_charset('utf8');
  </code></pre>
  
  <p>to</p>
  
  <pre><code>$mysqli-&gt;set_charset('utf8mb4');
  </code></pre>
  
  <p>made sure my php files are utf8 (I'm using Visual Studio Code so the files are created in UTF-8 by default), and php/html headers are set to utf8:</p>
  
  <p>index.php</p>
  
  <pre><code>header('Content-type: Text/HTML; Charset=UTF-8');
  </code></pre>
  
  <p>main.php (included at the end of index.php)</p>
  
  <pre><code>&lt;meta http-equiv="Content-Type" content="Text/HTML" /&gt;
  &lt;meta charset="UTF-8" /&gt;
  </code></pre>
  
  <p>The problem is that for some tables I have to manually insert the data, and this data is stored as is: with special characters, with accents, ñ, etc... And when I display this data in my website I can see that these characters <code>�</code> have replaced the special/accented characters.</p>
  
  <p>So my question is: is there any way to store data as is (without replacing/converting special/accented characters) in mysql and be able to display it fine (as is)?</p>
  
  <p>If I revert to <code>$mysqli-&gt;set_charset('utf8');</code> the data is displayed fine... So this keeps me wondering that there should be no problem with storing utf-8 characters as they are and there is some codification problem somewhere...</p>
  
  <p>I'm using sqlyog community (with wine) <s>and I read somewhere that sometimes the gui does not work correctly when you change some db/table configuration and the only way is the old way (running yourself the query), but I didn't tried this yet</s>. I ran queries to set the charset and collation of all tables/columns.</p>
  
  <p>What do you think?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm starting to think that mysqli does not accept utf8mb4 as a valid character encoding and uses utf8 from php and not from mysql... I also think mysql fckd up creating utf8mb4 instead of updating the existing utf8 to support 4 bytes....</p>
  
  <p>As I'm testing with mysqli charset utf8, everything is stored as is and displayed as is (with mysql charset and collation set to utf8mb4...).</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <pre><code>SELECT name, HEX(name) FROM person LIMIT 1
  </code></pre>
  
  <p>This is what it outputs:</p>
  
  <pre><code>New Person has name Altaïr 416C7461C3AF72
  </code></pre>
  
  <p>But as I already said, this is using:</p>
  
  <pre><code>$mysqli-&gt;set_charset('utf8');
  </code></pre>
  
  <p>to insert and to select. If I use utf8mb4 instead this is what it gets stored:</p>
  
  <pre><code>AltaÃ¯r
  </code></pre>
  
  <p>But it's displayed ok. What it's not displayed ok is if the name is stored as is, the displayed name will be <code>Alta�r</code>.</p>
  
  <p>So the question is: Why is mysqli/mysql storing <code>ï</code> as <code>Ã¯</code> using utf8mb4? And why is php displaying special characters like <code>ï</code> as <code>�</code> when utf8mb4 is set in mysqli?</p>
  
  <p>Can someone please confirm that <code>mysqli::set_charset</code> accepts utf8mb4 as a valid encoding?</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>I have a class function that selects a string from a table "es", for example: <code>Iniciar Sesión</code> (this is what's stored) and if mysqli charset is utf8, what is being selected/displayed is <code>Iniciar SesiÃ³n</code>.</p>
  
  <p>This maybe a completely different problem but it's clearly another codification problem. From my understanding, if the tables/columns are utf8mb4 and mysqli is set to utf8, mysql has to encode from utf8 (3bytes) to ut8mb4 (full byte support). So this means that <code>mysqli</code> does not use utf8 from php but from mysql. Is this correct, right?</p>
  
  <p>My application is currently having a rough time with encodings... (but maybe is some server configuration problem...)</p>
  
  <p><strong>UPDATE 4</strong></p>
  
  <p>Can the problem be here? I really have no idea about this kind of configurations:</p>
  
  <pre><code>SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
  +--------------------------+--------------------+
  | Variable_name            | Value              |
  +--------------------------+--------------------+
  | character_set_client     | utf8               |
  | character_set_connection | utf8               |
  | character_set_database   | utf8mb4            |
  | character_set_filesystem | binary             |
  | character_set_results    | utf8               |
  | character_set_server     | latin1             |
  | character_set_system     | utf8               |
  | collation_connection     | utf8_general_ci    |
  | collation_database       | utf8mb4_unicode_ci |
  | collation_server         | latin1_swedish_ci  |
  +--------------------------+--------------------+
  10 rows in set (0.00 sec)
  </code></pre>
  
  <p><strong>UPDATE 4-1/2</strong>  (copied from a comment)</p>
  
  <pre><code>CREATE TABLE `es` (
      id int(11) NOT NULL AUTO_INCREMENT, 
      name varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL, 
      text varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, 
      PRIMARY KEY (id), 
      UNIQUE KEY name (name)
  ) ENGINE=InnoDB AUTO_INCREMENT=76 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci` 
  </code></pre>