-1

Just a simple question.. I am using UTF-8 as standard in both my MySQL database and HTML file.. One thing that I don't understand is why a letter is shown differently in SQL than when I typed it. I am sure it should be placed the exact same way as I write it, when using UTF-8.

Is there a class or something that automatically keeps all posted data in UTF-8 and then posts it as it is?

Anders
  • 160
  • 1
  • 1
  • 9
  • Please do not edit an answer into the question. If you have solved the problem independently of the answers below, or using multiple different answers no particular one primarily, write your own answer and accept it. – Joe Jul 29 '13 at 01:52
  • Thanks for the answer and the linking. It is informative and I learned a lot from it, but seriously, the title doesn't describe anything of my problems, which obviously doesn't make it something I would search for.. How should I know this was the answer? But thanks anyway.. – Anders Jul 29 '13 at 18:19

3 Answers3

0

Your text editor also has an effect on your character encoding. Be sure to use a text editor or IDE that you can set the character encoding to UTF-8.

Expedito
  • 7,771
  • 5
  • 30
  • 43
  • text editor? I am using Dreamweaver if thats what you are referring to.. – Anders Jul 28 '13 at 12:57
  • Here's a link about character encoding in Dreamweaver: http://www.adobe.com/support/documentation/en/dreamweaver/mx2004/dwusing_errata/dwusing_errata2.html. And more info: http://superuser.com/questions/46693/dreamweaver-reverts-to-western-encoding-after-its-been-changed-to-utf8 – Expedito Jul 28 '13 at 13:03
  • hey.. thanks for your answer.. the thing I needed was to add this line after my PDO connection `, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")`. Hereafter everything went fine.. – Anders Jul 28 '13 at 16:24
0

Try this 1. method if not succes 2.metod. Last method i say open HTML File with notepad2 editor and change encoding from File menu to UTF8 and save. i think it will be solution;

  1. Method

    $dsn = $config ["driver"] . ':dbname=' . $config ['dbname'] . ';host=' . $config ['host']; 
        . ';charset=UTF-8'; //---> this is the solution
        try {
            $this->link = new PDO ( $dsn, $config ['user'], $config ['pass'] );
            // The mysql driver ignores the charset directive in $dsn (yeah, that sucks)
            if ($config ["driver"] == 'mysql')
                $this->link->exec ( "set names utf8" );
        }
        catch ( PDOException $e ) {
            die ( "Hata var : " . $e->getMessage () );
        }

Other:


$encodings = array("windows-1254","utf-8", "iso-8859-9" );
$data = file_get_contents($TempSrc);
$data=mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data, $encodings));

0

MY SOLUTION In case anybody experience similar problem, mine was solved by adding this line after the PDO connection , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'").

Now the connection string looks like this:

$conn = new PDO('mysql:host=localhost;dbname=$dbname', $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

Anders
  • 160
  • 1
  • 1
  • 9