Search code examples
phpmysqlencodingutf-8collate

PHP string is not converted to utf 8 when selected from mysql


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.


Solution

  • 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;