Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Data Storage</strong>:</p> <ul> <li><p>Specify the <code>utf8mb4</code> character set on all tables and text columns in your database. This makes MySQL physically store and retrieve values encoded natively in UTF-8. Note that MySQL will implicitly use <code>utf8mb4</code> encoding if a <code>utf8mb4_*</code> collation is specified (without any explicit character set).</p></li> <li><p>In older versions of MySQL (&lt; 5.5.3), you'll unfortunately be forced to use simply <code>utf8</code>, which only supports a subset of Unicode characters. I wish I were kidding.</p></li> </ul> <p><strong>Data Access</strong>:</p> <ul> <li><p>In your application code (e.g. PHP), in whatever DB access method you use, you'll need to set the connection charset to <code>utf8mb4</code>. This way, MySQL does no conversion from its native UTF-8 when it hands data off to your application and vice versa.</p></li> <li><p>Some drivers provide their own mechanism for configuring the connection character set, which both updates its own internal state and informs MySQL of the encoding to be used on the connection&mdash;this is usually the preferred approach. In PHP:</p> <ul> <li><p>If you're using the <a href="http://www.php.net/manual/en/book.pdo.php" rel="noreferrer">PDO</a> abstraction layer with PHP &ge; 5.3.6, you can specify <code>charset</code> in the <a href="http://php.net/manual/en/ref.pdo-mysql.connection.php" rel="noreferrer">DSN</a>:</p> <pre><code>$dbh = new PDO('mysql:charset=utf8mb4'); </code></pre></li> <li><p>If you're using <a href="http://www.php.net/manual/en/book.mysqli.php" rel="noreferrer">mysqli</a>, you can call <a href="http://php.net/manual/en/mysqli.set-charset.php" rel="noreferrer"><code>set_charset()</code></a>:</p> <pre><code>$mysqli-&gt;set_charset('utf8mb4'); // object oriented style mysqli_set_charset($link, 'utf8mb4'); // procedural style </code></pre></li> <li><p>If you're stuck with plain <a href="http://php.net/manual/en/book.mysql.php" rel="noreferrer">mysql</a> but happen to be running PHP &ge; 5.2.3, you can call <a href="http://php.net/manual/en/function.mysql-set-charset.php" rel="noreferrer"><code>mysql_set_charset</code></a>.</p></li> </ul></li> <li><p>If the driver does not provide its own mechanism for setting the connection character set, you may have to issue a query to tell MySQL how your application expects data on the connection to be encoded: <a href="http://dev.mysql.com/doc/en/charset-connection.html" rel="noreferrer"><code>SET NAMES 'utf8mb4'</code></a>.</p></li> <li><p>The same consideration regarding <code>utf8mb4</code>/<code>utf8</code> applies as above.</p></li> </ul> <p><strong>Output</strong>:</p> <ul> <li><p>If your application transmits text to other systems, they will also need to be informed of the character encoding. With web applications, the browser must be informed of the encoding in which data is sent (through HTTP response headers or <a href="https://stackoverflow.com/q/4696499">HTML metadata</a>).</p></li> <li><p>In PHP, you can use the <a href="http://www.php.net/manual/en/ini.core.php#ini.default-charset" rel="noreferrer"><code>default_charset</code></a> php.ini option, or manually issue the <code>Content-Type</code> MIME header yourself, which is just more work but has the same effect.</p></li> </ul> <p><strong>Input</strong>:</p> <ul> <li><p>Unfortunately, you should verify every received string as being valid UTF-8 before you try to store it or use it anywhere. PHP's <a href="http://php.net/manual/en/function.mb-check-encoding.php" rel="noreferrer"><code>mb_check_encoding()</code></a> does the trick, but you have to use it religiously. There's really no way around this, as malicious clients can submit data in whatever encoding they want, and I haven't found a trick to get PHP to do this for you reliably.</p></li> <li><p>From my reading of the current <a href="http://whatwg.org/html" rel="noreferrer">HTML spec</a>, the following sub-bullets are not necessary or even valid anymore for modern HTML. My understanding is that browsers will work with and submit data in the character set specified for the document. However, if you're targeting older versions of HTML (XHTML, HTML4, etc.), these points may still be useful:</p> <ul> <li><em>For HTML before HTML5 only</em>: you want all data sent to you by browsers to be in UTF-8. Unfortunately, if you go by the the only way to reliably do this is add the <code>accept-charset</code> attribute to all your <code>&lt;form&gt;</code> tags: <code>&lt;form ... accept-charset="UTF-8"&gt;</code>.</li> <li><em>For HTML before HTML5 only</em>: note that the W3C HTML spec says that clients "should" default to sending forms back to the server in whatever charset the server served, but this is apparently only a recommendation, hence the need for being explicit on every single <code>&lt;form&gt;</code> tag.</li> </ul></li> </ul> <p><strong>Other Code Considerations</strong>:</p> <ul> <li><p>Obviously enough, all files you'll be serving (PHP, HTML, JavaScript, etc.) should be encoded in valid UTF-8.</p></li> <li><p>You need to make sure that every time you process a UTF-8 string, you do so safely. This is, unfortunately, the hard part. You'll probably want to make extensive use of PHP's <a href="http://www.php.net/manual/en/book.mbstring.php" rel="noreferrer"><code>mbstring</code></a> extension.</p></li> <li><p><strong>PHP's built-in string operations are <em>not</em> by default UTF-8 safe.</strong> There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent <code>mbstring</code> function.</p></li> <li><p>To know what you're doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level. Check out any of the links from <a href="http://www.utf8.com/" rel="noreferrer">utf8.com</a> for some good resources to learn everything you need to know.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload