I have been struggling with encoding problems in MySQL for a while. I am building a database that will contain not only Latin but Cyrillic and Arabic text as well. So here is an example on how I create the database:
CREATE DATABASE db1
DEFAULT CHARACTER SET utf8
COLLATE utf8_unicode_ci;
Then a table:
CREATE TABLE TempTb1
(
ID INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
arabic VARCHAR(100) NOT NULL
)
DEFAULT CHARACTER SET utf8
COLLATE utf8_unicode_ci;
And when I put some data and select it I get only some strange characters. So I wrote a small PHP script to test it but it doesn't work either:
<?php
header('Content-type: text/plain; charset=utf-8');
$a = mysql_connect('localhost','root','') or die('Problem connecting to database!');
$b = mysql_select_db('db1') or die('Problem selecting database');
mysql_set_charset('utf8');
mysql_query("set names 'utf8'");
mysql_query('set character set utf8');
$query = mysql_query("SELECT * FROM Tb1;");
while($row = mysql_fetch_assoc($query))
{
$id = $row['ID'];
$name = $row['name'];
$arabic = $row['arabic'];
echo $id.' '.$name.' '.$arabic.PHP_EOL;
}
?>
I have tested with both utf8_unicode_ci
and utf8_general_ci
. What could be wrong? BTW I have EasyPHP 5.2.10.