-5

Possible Duplicate:
UTF-8 all the way through

Hi I recently switched from linux to freebsb on my server, and now my database is acting up.

When I try to echo a string containing å,ä or ö (swedish letters) it becomes a questionmark. ie: Söndag becomes S�ndag. echo "ä"; works. $ file --mime test.php test.php: text/plain; charset=utf-8

test.php

<?php
$a="å";
mysql_connect("localhost", "root", ":-)");
mysql_select_db("lidev");

$result=mysql_query("select * from DLG where dag='Onsdag'");
$row=mysql_fetch_array($result);

echo $row['dagens'];
echo "<br>";
echo mb_detect_encoding($row['dagens']);
?>

mb_detect_encoding() outputs "UTF-8" My mysql table is in utf8_general_ci.

I'm completely stuck! What's wrong?

Cheers!

Community
  • 1
  • 1
user1469360
  • 1
  • 1
  • 2

1 Answers1

2

seems like you're not using utf-8 everywhere so your data got messed up at some point. depending on what exactly you're doing, you'll have to change/add one or more of the following points (most likely it's the SET CHARSET/mysql_set_charset you forgot):

  • tell MySQL to use utf-8. to do this, add this to your my.cnf:

    collation_server = utf8_unicode_ci
    character_set_server = utf8
    
  • before interacting with mysql, send this two querys:

    SET NAMES 'utf8';
    CHARSET 'utf8';
    

    or, alternatively, let php do this after opening the connection:

    mysql_set_charset('utf8', $conn); // when using the mysql_-functions
    mysqli::set_charset('utf8') // when using mysqli
    
  • set UTF-8 as the default charset for your database

    CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8';
    
  • do the same for tables:

    CREATE TABLE `my_table` (
      -- ...
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
  • assuming the client is a browser, serve your content as utf-8 and the the correct header:

    header('Content-type: text/html; charset=utf-8');
    

    to be really sure the browser understands, add a meta-tag:

    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    
  • and, last but not least, tell the browser to submit forms using utf-8

    <form accept-charset="utf-8" ...>
    
oezi
  • 51,017
  • 10
  • 98
  • 115
  • 2
    why in the hell post a duplicate answer??? – dynamic Jun 20 '12 at 13:40
  • 1
    when i started writing this, there was no "close as duplicate"-vote and i won't delete an answer just because i recognise this later, as it might still be helpful. indeed, you're right: this question is a duplicate and that's why i also voted to close it. – oezi Jun 20 '12 at 13:44