Thursday, May 17, 2012

UTF8 Unicode PHP MySQL for International Characters

This is a note to myself but also to anyone out there who's spent as long as I did looking for a solution to this. Maybe it's just me but this is the scenario:
  1. Joomla works fine with international characters
  2. A Module Extension I wrote for Joomla! displayed funny characters.
  3. The Joomla! articles were displaying the correct characters for that language set.
  4. I needed to make my extension read data from a MySQL database and display the caracters as intended with UTF8.

I tried enough extensions and forum solutions, and although these changes would have an effect on the module (such as take away accents and convert to ASCII), they weren't what we were looking for.

The quick solution was to make the script run a MySQL command at the start:
SET NAMES 'utf8'
Now I need to run this command from within a Joomla! extension using the mysql Joomla! classes, here's how I've used in this script pulling IDs and titles from the a sample table:
$db =& JFactory::getDBO();
$sql_utf8 = "set names 'utf8'";
$db->setQuery( $sql_utf8 );
$temp_result = $db->query();
$sql = 'SELECT '.$modulescancontentsql.' FROM #__content WHERE state=1';
$db->setQuery( $sql );
$rows = $db->loadObjectList();
foreach( $rows as $row ) {
    $id = $row->id;
    $title = $row->title;
}
This fix has so far worked for Arabic, Chinese, Hebrew, Russian, and Slovak. Fingers crossed this should work for all characters that Joomla! can handle by default anyway.

Cause: Q. Why does this work for Joomla! articles and not for this module? A. Think this is because that module is loaded before... not sure but for some reason the above command set names 'utf8' is obviously not run prior to the module being displayed.

Add comment

Please note: all comments are reviewed before being published.


Security code
Refresh

Member Login

Joes Latest Members

A huge WELCOME goes to today's newest members:

  • infonat
Member Signups (Activated)
BeforeCurrentTrend
Day21=
Week105ê
Month11224ê
Year2,265612ê

Member Stats
54 guests are currently online.
1,178 members are still deciding.
There are 5,935 members in total.

Latest Comments

Paypal Donations

Want to support my work? Any donation is a blessing :c)