title: Inserting UMLAUT into mysql database using  a php script
body: <p>i am developing a script which takes a csv as an input, it then reads the file and insert its contents to a mysql database. So the problem comes while inserting the data to the database. It converts UMLAUT into random characters.</p>
  
  <p>FYI -- My database is in latin_german_ci. [ i have also tried changing it to UTF8]</p>
  
  <p>i am able to display UMLAUT characters in web browsers but when i try to insert them in a database through a sql query, it inserts random characters.</p>
  
  <pre><code>&lt;?php
  
  function uploadCsv($filename){
  
      echo "filename - ".$filename."&lt;br/&gt;";
  
      if(isset($filename) || $filename == ""){ // return with an error msg.
  
      }else{
          $pos = stripos($filename, ".csv");
          if($pos == 0 || $pos != strlen($filename)-4){
              //echo "invalid format";
              return ;
          }
      }
  
      set_time_limit(0);
      $error = "";
      $row = 0;
      $handle = fopen($filename, "r");
      //echo "&lt;br/&gt;".$filename."&lt;br/&gt;";
      //echo "&lt;br/&gt;".$handle."&lt;br/&gt;";
      if($handle == null){
  
          return "unable to process";
      }
  
      while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
  
          if ($row == 0) {
              // this is the first line of the csv file
              // it usually contains titles of columns
              // do nothing.
              $num = count($data);
              //echo "num - ".$num."&lt;br/&gt;";
              if($num != 2){
                  // echo "returning back";
                  return "invalid CSV format.";
              }
          }
          // this handles the rest of the lines of the csv file
          $num = count($data);
          $id = $data[0];
          $inserQuery = "";
          $inserQuery =  "INSERT INTO `table` (
                              `ID` ,
                              `Productname`
                              )
                              VALUES (";
  
          for ($c=0; $c &lt; $num; $c++) {
              if($c==0){
                  $inserQuery .= " '". utf8_encode($data[$c])."'" ;
              }else{
                  $inserQuery .= ", '". utf8_encode($data[$c]) ."'" ;
              }
          }
          $inserQuery .= ");";
          echo $inserQuery."&lt;br/&gt;";
          mysql_query($inserQuery);
          if(mysql_affected_rows() == -1 || mysql_affected_rows() &lt;1){
              echo "error&lt;br/&gt;";
          }else{
              echo "row inserted - ".$row." with ID = ".$id." &lt;br/&gt;";
          }
          $row++;
      }
      fclose($handle);
      return "1";
  }
  ?&gt;
  </code></pre>
  
  <p>Please help....</p>
  
  <p>thanks</p>