I'm having a bit of a language problem.

I hav ea simple form where people from all over the world put in some data.
The data is put into a MySQL database.

But how do I habdle all the different characters like ñ ê ü and so on.

My database is utf8_unicode_ci and it seems like the correct characters is put into the database when I see it in MyPHPAdmin but when I get the data on my site many of the characters is replaced with a � instead...

How do I get the characters correctly from the database ?

    Your table fields should have the same collation as you HTML character encoding.

    e.g. if your HTML charset is UTF-8 then you table fileds should have UTF-8 collation.

      But what charset should I choose if people from all over the world enters data into teh database.
      So all special characters like ñ ê æ ø å ä ö etc.

        If you use the charset META tag below in your webpage, the characters should display fine.

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

        If you have other charsets you want to display on the same page, you can use bogu's suggestion.

          The challenge is to get every piece of the process to "talk" in the same character set. Here's a little script that puts a string into the DB as UTF-8, retrieves it, and outputs it to a HTML page that has UTF-8 defined as its character set:

          <?php
          // sample text:
          $text = <<<END
          ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ &#161; &#162; &#163; &#164; &#165; &#166; &#167; &#168; &#169; &#170; &#171; &#172; &#173; &#174; &#175; &#176; &#177; &#178; &#179; &#180; &#181; &#182; &#183; &#184; &#185; &#186; &#187; &#188; &#189; &#190; &#191; &#192; &#193; &#194; &#195; &#196; &#197; &#198; &#199; &#200; &#201; &#202; &#203; &#204; &#205; &#206; &#207; &#208; &#209; &#210; &#211; &#212; &#213; &#214; &#215; &#216; &#217; &#218; &#219; &#220; &#221; &#222; &#223; &#224; &#225; &#226; &#227; &#228; &#229; &#230; &#231; &#232; &#233; &#234; &#235; &#236; &#237; &#238; &#239; &#240; &#241; &#242; &#243; &#244; &#245; &#246; &#247; &#248; &#249; &#250; &#251; &#252; &#253; &#254; &#255;
          END;
          
          // convert the text to UTF-8:
          $text = mb_convert_encoding($text, 'UTF-8');
          
          // connect to DB and tell it we want to use UTF-8:
          $db = new mysqli('localhost','xxxxx','xxxxx','test');
          $db->query("SET NAMES 'utf8'");
          $db->query("SET CHARACTER SET utf8");
          
          // put data into DB, retrieve it, then delete the row:
          $db->query("INSERT INTO test (text) VALUES('" . $db->escape_string($text) . "')");
          $res = $db->query('SELECT * FROM test');
          $row = $res->fetch_assoc();
          $output = $row['text'];
          $db->query("DELETE FROM test WHERE 1");
          
          // make sure we're using UTF-8 for our page output:
          header('Content-Type: text/html;charset=UTF-8');
          
          ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
          <html lang='en'>
          <head>
          <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
          <title>Untitled</title>
          </head>
          <body>
          <h3>Before:</h3>
          <p>
          <?php
          // show text before it was sent to DB:
          echo htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
          ?>
          </p>
          <h3>After:</h3>
          <p>
          <?php
          // show what we got from the DB:
          echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
          ?>
          </p>
          </body>
          </html>
          
            Write a Reply...