Friday, July 20, 2012

Special character display and language encoding in MYSQL



To force display HTML page in utf-8, we can use
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


To set the MySQL encoding default to UTF-8, we need to add the following to my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

[mysqld]
default-character-set = utf8    
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
 
Sometimes we still find that  non-English characters 
are output as question marks... Try to useysql_query("SET NAMES 'utf8'");
Example

<?php

$db = mysql_connect('DB_ADDRESS','DB_USER','DB_PASS') or die("Database error");

mysql_select_db('My_DB', $db);

//SOLUTION::  add this comment before your 1st query -- force multiLanuage support
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");


$query = "select * from DB_TABLE";

$result = mysql_query($query);


?>

No comments:

Post a Comment