0

I have a database issue, that I'm unable to understand. I'm from Denmark and have made a sign-up system in PHP and MySQL. Now... I have made two tables seperately.

One of the tables (let's call it table1) displays my beloved danish letters (æøå) just fine, when I'm querying them from the database through PHP. But when I go to phpMyAdmin, then the letters are displayed wierdly... For instance: It looks like this in phpMyAdmin:

Bjørn (which is Bjørn)

But the again, when I get them from the database with a mysql_query('SELECT * FROM $tablename'), then it is displayed as 'Bjørn' (as it should).

Now to the problem...

In the other table (let's call it table 2), then in phpMyAdmin 'Bjørn' is displayed as 'Bjørn' (what seems correct). But when I pull it into PHP with mysql_query('SELECT * FROM $tablename') then it is displayed as 'Bj?rn'. All of the letters 'æøå' is displayed as a '?'.

I tried doing a SHOW TABLE STATUS, and it shows that the Collation is the same.

In table1, then the variables are VARCHAR(255), while in table2, the variables are TEXT.

Both tables are created like this:

CREATE TABLE >>tablename<< ( bla bla bla ) CHARSET=UTF8 
2

2 Answers 2

2

You should connecto mysql like this

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

And then try executing the query it fetches properly

The problem here is , you should specify the charset while you are connecting to DB also .

Even while storing also your inserts gets garballed if you dont set the charset in your connection to utf8 so do verify once whether you are setting like this while connecting to DB or not .

Hope this helps

Also last but not least , while displaying in the browser also you should set html headers while dumping the data from DB to browser like below

<?php 
 header('Content-type: text/html; charset=utf-8'); 
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know what's wrong with phpmyadmin, but in my own programming I do the following things:

PHP (before anything else):

header("Content-Type: text/html; charset=utf-8");

HTML:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

DB query:

SET CHARACTER SET utf8;

Or if using PDO my dsn looks like:

mysql:dbname=mydb;host=localhost;charset=utf8

2 Comments

So do I have to write 'SET CHARACTER SET utf8;' into all my queries?
no..after connection you send this query one time to db. It's the same like mysql_set_charset('utf8'). I recommend not to use mysql_* functions. Better use mysqli_* or PDO. mysql_* functions will be deprecated and not available in future PHP releases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.