title: mysql default charset different when invoked by php
body: <p>As many others, I'm having some problems with mysql charset. As many others, I want everything to be UTF-8, but mysql was installed with latin-1, and no matter how I try/google/experiment with mysql config there is still latin-1 lurking in client settings. </p>
  
  <p>Ok, here is the setup. I have a (non-root) mysql user 'usr' with a password 'pwd'. Whenever I access mysql via terminal (mysql -uusr -p) and then ask him nicely about his charsets, he tell that he is in love with utf8 (as he ought to be):</p>
  
  <pre><code>mysql&gt; SHOW VARIABLES LIKE 'character%';
  +--------------------------+----------------------------+
  | Variable_name | Value |
  +--------------------------+----------------------------+
  | 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 |
  | character_sets_dir | /usr/share/mysql/charsets/ |
  +--------------------------+----------------------------+
  8 rows in set (0.00 sec)
  
  mysql&gt; SHOW VARIABLES LIKE 'collation%';
  +----------------------+-----------------+
  | Variable_name | Value |
  +----------------------+-----------------+
  | collation_connection | utf8_general_ci |
  | collation_database | utf8_unicode_ci |
  | collation_server | utf8_unicode_ci |
  +----------------------+-----------------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>However, if I use PHP to access mysql (via the very same user):</p>
  
  <pre><code>$mysql_link=mysql_connect('localhost','usr','pwd');
  $result1=mysql_query("show variables like 'character%'");
  $result2=mysql_query("show variables like 'collation%'");
  mysql_close($mysql_link)
  </code></pre>
  
  <p>And print_r $result1, $result 2, it magically falls back to latin-1:</p>
  
  <pre><code>character_set_client =&gt; latin1
  character_set_connection =&gt; utf8
  character_set_database =&gt; utf8
  character_set_filesystem =&gt; binary
  character_set_results =&gt; latin1
  character_set_server =&gt; utf8
  character_set_system =&gt; utf8
  character_sets_dir =&gt; /usr/share/mysql/charsets/
  
  collation_connection =&gt; utf8_unicode_ci
  collation_database =&gt; utf8_general_ci
  collation_server =&gt; utf8_unicode_ci
  </code></pre>
  
  <p>This happens regardless whether I invoke php via browser (as php-cgi) or via terminal (as php-cli).</p>
  
  <p>Kinda fix for that is to set charset manually at each connection:</p>
  
  <pre><code>mysql_set_charset('utf8',$mysql_link);
  </code></pre>
  
  <p>That works. But I feel like there should be a way to do that via mysql config.</p>
  
  <p>For reference, Mysql config (my.cfg) includes:</p>
  
  <pre><code>[client]
  default_character_set = utf8
  
  [mysqld]
  init_connect='SET collation_connection = utf8_unicode_ci'
  character-set-server = utf8
  collation-server = utf8_unicode_ci
  </code></pre>
  
  <p>And PHP config (php.ini) includes</p>
  
  <pre><code> default_charset = "utf-8"
  </code></pre>
  
  <p>Thank forward! =)</p>
  
  <p>P.S. I know that mysql_ functions are deprecated and should be replaced with mysqli_ ones. But hopefully that doesn't have anything to do with this exact problem =)</p>