Mysql REPLACE() issue with Latin1 multi-byte character


I’ve a database with server collation of utf8 / utf8_bin. DB have arabic utf8 text with accent chars (zabar / kasar etc). I want to get text without accent chars.

For e.g. SELECT replace(field1,0x[CODE],”) — where [CODE] is the accent char.

REPLACE() is working fine when i write static text in place of field1. But it does not replace when it run at database field.


After reading an article, i come to know there are 2 (client & server) encodings used with mysql. My server encoding (table charset/collation) was utf8, but client’s encoding was latin1 and the time of INSERT.

So data stored in DB is somewhat mixed, due to which REPLACE() was not working as expected.


1. i fetched the data using php,
2. applied bin2hex() function on data,
3. set client’s encoding to utf8 (set names utf8) and
4. updated same record using mysql’s UNHEX function.

Here is sample code …

$q=mysql_query("SELECT field1 FROM table1 where id = 4");
$rs = mysql_fetch_assoc($q);
$v = bin2hex($rs["field1"]);
mysql_query("SET NAMES UTF8");
mysql_query("UPDATE table1 SET field1=UNHEX('$v') WHERE id = 4");

This fixed my client’s encoding to utf8 that matches with server encoding, now REPLACE() is removing that accent hex code and working as expected .

Following article really helped …

Share your thoughts & feedback

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: