title: Fixing messed up char encoding in MySQL
body: <p>There is server 1 and a database 1 where data is stored and queried not having MySQL connection encoding set. Everything works of course until it comes to integration.</p>
  
  <p>There is another server and database (2) that uses UTF8 encoded connection, so all values are stored properly.</p>
  
  <p>Some tables of db1 have to be copied to db2.</p>
  
  <p>This is where I got stuck with the task. I've tried <code>SELECT INTO</code> with multiple connections, then I got permission errors (I simply can't swap to the other user in a single query when needed)</p>
  
  <p>I've also tried something like this 
  <code>SELECT * INTO OUTFILE '/back.txt'
  FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
  FROM article"</code> which returned Access denied even though I checked all permissions I guess.</p>
  
  <p>So I decided to fetch the data manually with PHP, store it in arrays, then insert everything properly having the connection encoding set. This should work. Although it's way more complicated than anything else that I've been trying.</p>
  
  <p>These are the code fragments without sensitive information:</p>
  
  <blockquote>
    <p>Connection to server1, selection of DB1</p>
  </blockquote>
  
  <pre><code>for($id=0;$id&lt;=203;$id++){
  $q=mysql_query("SELECT * FROM article WHERE id=$id");
  $n=mysql_num_rows($q);
  $adat[$id]=mysql_fetch_assoc($q);
  }
  </code></pre>
  
  <blockquote>
    <p>Closing connection 1, connection to server2, selection of DB2</p>
  </blockquote>
  
  <pre><code>mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
  </code></pre>
  
  <p>Here it came to my mind that what if I could apply a query to a certain connection only? It would be easier. I don't know how this should be done, so I wrote this mess below:</p>
  
  <pre><code> foreach($adat as $k=&gt;$v) {
  
      if(is_array($v)) foreach($v as $k2=&gt;$v2) { echo $k." ".$v2;
      $puffer.=",'".$v2."'";
      } 
      $puffer=substr($puffer,1);
      echo $puffer;
      if(is_array($v)) mysql_query("INSERT INTO tarticles VALUES($puffer)");
      echo "&lt;br/&gt;";
  }
  </code></pre>
  
  <p>Aaaaand. It does not work. </p>
  
  <blockquote>
    <p>(I'm checking is_array because row ids don't always follow each other)</p>
  </blockquote>
  
  <p>Please help! </p>