title: a permanent way of doing mysqli->set_charset()?
body: <p>after setting all config file and runtime options for charset that i can find to utf-8, new mysqli connections made with php still has its charset set to latin1, which effectively means that i have to call <code>$mysqli-&gt;set_charset('utf8')</code> each time i connect.</p>
  
  <pre><code>$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);  
  if ($mysqli-&gt;connect_error)  
    err_handle("mysql connect error({$mysqli-&gt;connect_errno}).");  
  if (!$mysqli-&gt;set_charset("utf8"))  
    err_handle("db error({$mysqli-&gt;errno}).");
  </code></pre>
  
  <p>i wonder if there is a permanent way of doing this?</p>
  
  <p>similar problem was encountered in <a href="https://stackoverflow.com/questions/3571752/why-do-i-have-to-use-set-charsetutf8-even-though-everything-is-utf-8-encoded">this post</a>.</p>
  
  <hr>
  
  <p>a "<code>show variables like 'character_set%'</code>" query on the mysql server before calling <code>$mysqli-&gt;set_charset('utf8')</code> shows:<br>
  (this part was ambiguous in previous revs)</p>
  
  <pre><code>character_set_client    latin1  
  character_set_connection    latin1  
  character_set_database  utf8  
  character_set_filesystem    binary  
  character_set_results   latin1  
  character_set_server    utf8  
  character_set_system    utf8  
  </code></pre>
  
  <p>the client, connection and results charset can only be changed to utf8 with <code>$mysqli-&gt;set_charset('utf8')</code> at runtime. after that it shows:</p>
  
  <pre><code>character_set_client    utf8  
  character_set_connection    utf8  
  character_set_database  utf8  
  character_set_filesystem    binary  
  character_set_results   utf8  
  character_set_server    utf8  
  character_set_system    utf8  
  </code></pre>
  
  <p>i have  </p>
  
  <pre><code>default_charset = "utf-8"
  </code></pre>
  
  <p>set in php.ini, and  </p>
  
  <pre><code>[client]  
  default-character-set=utf8  
  ...  
  [mysqld]  
  ## This option is deprecated in favor of --character-set-server.
  #default-character-set=utf8  
  </code></pre>
  
  <p>set in my.cnf.</p>
  
  <p>the default charset for my tables is also utf8.</p>
  
  <p>seems like the "[client]" options only affect the cmd "mysql" tool and have nothing to do with php.</p>
  
  <p>the return value of <code>$mysqli-&gt;character_set_name()</code> is always <strong>latin1</strong> no matter what i do, until <code>$mysqli-&gt;set_charset('utf8')</code> is called.</p>
  
  <p>i guess "latin1" is a mysql thing, since i cant recall anything else that defaults to "latin1" on my system.</p>
  
  <p>^<em>update:</em> according to mysql manual <a href="http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html" rel="nofollow noreferrer">9.1.4</a>, <a href="http://dev.mysql.com/doc/refman/5.1/en/charset-applications.html" rel="nofollow noreferrer">9.1.5</a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_character_set_client" rel="nofollow noreferrer">5.1.3</a>, <code>character_set_client</code> should be provided by the client. i guess php doesn't provide it upon connection and mysql uses the fall-back charset <em>latin1</em>.</p>
  
  <p>i'm running php 5.3 on debian wheezy with mysql 5.1.</p>
  
  <p>any suggestion?</p>
  
  <hr>
  
  <p>updated with info from comments:</p>
  
  <p>i forgot to mention the <code>skip-character-set-client-handshake</code> directive and why i was reluctant to use it.</p>
  
  <p>upon first sight i thought ignoring the handshake might result in the situation that the client talks <em>latin1</em> while the server talks <em>utf8</em>. how does the server convert the string from charset <code>character_set_client</code> to <code>character_set_server</code> without knowing the charset currently in use?</p>
  
  <p>correct me if i'm wrong, plz. <del>i will experiment with this setting later today to see if it works.</del></p>
  
  <p>Updated with <strong>workaroud</strong>:</p>
  
  <p>make sure everything works under utf-8 (or any preferable charset). then add the <code>skip-character-set-client-handshake</code> line to <code>my.cnf</code>.</p>
  
  <p>this works for me so far. i experimented with some double-width utf-8 characters. both <code>insert</code> and <code>select</code> succeeded and displayed properly in the browser.</p>
  
  <p>what skipping the handshake means is still unclear. and the mysql server now becomes uncapable of using any charset except utf-8, whick makes this workaround quite impractical since i simply cant apply this setting to all the servers that my website runs on.</p>
  
  <p>so i'm not adopting this workaround. further comments and answers are much appreciated.</p>