1

I have the following script:

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

$rq = "SELECT `name` FROM `mapamond_countries` WHERE `id` = 93";
$str = $db->GetAll($rq);

var_dump($str[0]['name']);
var_dump("شيلى");

The string شيلى is copy pasted from database (phpmyadmin).

First var_dump: string(25) "بلجيكا"

Second var_dump: string(8) "شيلى"

Can any1 explain why is this difference and how to fix it? The DB collate, table collate and column collate is utf8_unicode_ci.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Marcus Adams Feb 25 '15 at 14:59

2 Answers2

1

Try:

1- run this query ALTER DATABASE mydatabasename charset=utf8;

2-use SET NAMES utf8 before you do any query

3-use DEFAULT CHARSET=utf8 when creating new tables

to alter the table

ALTER TABLE `tableName` CHARACTER SET utf8;
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
  • Isn't character set a column attribute, and when setting on the table, you're just setting the default for new columns? – Marcus Adams Feb 25 '15 at 14:58
  • The sysadmin changed the db collation so all content inseted b4 was wrong. Used SET COLLATION_CONNECTION to the old one, and set names to utf8 and is ok now. – zozo Feb 25 '15 at 15:31
0

"بلجيكا" is the "double encoding" for "شيلى". SELECT HEX(col)... from the table to see what you have. "C398C2B4C399C5A0C399E2809EC399E280B0" is the double encoded value for what the hex should be: "D8B4D98AD984D989". Double encoding is discussed in http://mysql.rjweb.org/doc.php/charcoll . If Osama's answer fails to fix the data; read that blog and/or let's discuss the issue further.

Rick James
  • 135,179
  • 13
  • 127
  • 222