
Charset Problem
The image above is showing part of a small hack I coded for a phpBB board to show topics from selected sections. Those question marks were suppose to be Arabic letters, but after a recent update to phpBB the hack failed to use the board config file to connect so I had to set up my own direct connection to the DB.
The connection was fine, but I got “?????” instead of something I can read in Arabic. After some tests I figured out, though the MySQL DB is using UTF-8 as a character set, which means it can handle the language efficiently, I had to set the character set to UTF-8 via PHP for every query using the following code:
1 | mysql_query("SET CHARACTER SET utf8"); |
What is even better is setting the character set at once with a single line of code for the whole connection by using the following code:
1 | mysql_query("SET CHARACTER SET 'utf8'", $db); |
This code should be used directly after the connection, as in the following example:
1 2 3 4 5 6 7 8 | $db = mysql_connect('localhost','DB_USER','DB_PASSWORD') or die("Could not connect"); mysql_select_db('DB_NAME',$db) or die("Could not connect"); // Set character set to UTF-8 // mysql_query("SET CHARACTER SET 'utf8'", $db); |
Now I got my topics in readable Arabic once again!

Charset Problem Solved
