<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Character sets</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlinfo.concepts.buffering.html">Buffered and Unbuffered queries</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mysql.html">Mysql</a></div> <div class="up"><a href="mysqlinfo.concepts.html">Concepts</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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">» 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">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.html" class="function">mysqli_real_escape_string()</a></span> for mysqli, <span class="function"><a href="function.mysql-real-escape-string.html" class="function">mysql_real_escape_string()</a></span> for mysql, and <span class="methodname"><a href="pdo.quote.html" 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-1559"> <p><strong>Example #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"><?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->real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SET NAMES utf8"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Will not affect $mysqli->real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SET CHARACTER SET utf8"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// But, this will affect $mysqli->real_escape_string();<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'utf8'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></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 each API. </p> <div class="example" id="example-1560"> <p><strong>Example #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"><?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 />if (!</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'utf8'</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 utf8: %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">error</span><span style="color: #007700">);<br />} else {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Current character set: %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">character_set_name</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">( </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">get_charset</span><span style="color: #007700">() );<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-1561"> <p><strong>Example #3 Setting the character set example: <a href="ref.pdo-mysql.connection.html" class="link">pdo_mysql</a></strong></p> <div class="example-contents"><p> Note: This only works as of PHP 5.3.6. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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=utf8"</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">?></span> </span> </code></div> </div> </div> <div class="example" id="example-1562"> <p><strong>Example #4 Setting the character set example: mysql</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_connect</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_pass"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br />if (!</span><span style="color: #0000BB">mysql_set_charset</span><span style="color: #007700">(</span><span style="color: #DD0000">'utf8'</span><span style="color: #007700">, </span><span style="color: #0000BB">$conn</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Error: Unable to set the character set.\n"</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">mysql_client_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlinfo.concepts.buffering.html">Buffered and Unbuffered queries</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mysql.html">Mysql</a></div> <div class="up"><a href="mysqlinfo.concepts.html">Concepts</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>