Have you ever got problem that you got broken/unknown character when you stored string data especially HTML string data to MySql database? I've got that problem few days ago. I built program with Java as main string data processor, PHP as data storing tool, and MySql as database. I tried some encoding function in Java and PHP but I still didn't know why there're broken character like "â€Å“", "â€", "’", or others in database.
That characters represent uncommon/special characters like £, €, or special quote symbol which doesn't have HTML code representation like “, ”, or others. After doing some inspections I figured out the problem. The problem is mysqli_query() and other PHP storing functions didn't store string data from PHP program properly.
HTML string data commonly use UTF-8 character set encoding which can maintain many special characters. So I tried following PHP functions to built connection with MySql.
$mysqli = new mysqli("my_host", "my_user", "my_password", "my_db");
$mysqli->query("SET NAMES utf8");
or
$mysqli->query("SET CHARACTER SET utf8");
When you store some string data in PHP using mysql_query("INSERT INTO ... VALUES '$string_data' ... ") the "$string_data" will be handled by using UTF8 character set encoding. UTF8 encoding represents Unicode character into some btyes (8 bits). If character code is more than 255 then it uses more than one byte to store character code.
Comments
Post a Comment