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:
copyraw
SET NAMES 'utf8'
  1.  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:
copyraw
$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;
}
  1.  $db =& JFactory::getDBO()
  2.  $sql_utf8 = "set names 'utf8'"
  3.  $db->setQuery( $sql_utf8 )
  4.  $temp_result = $db->query()
  5.  $sql = 'SELECT '.$modulescancontentsql.' FROM #__content WHERE state=1'; 
  6.  $db->setQuery( $sql )
  7.  $rows = $db->loadObjectList()
  8.  foreach( $rows as $row ) { 
  9.      $id = $row->id; 
  10.      $title = $row->title; 
  11.  } 
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.

Category: Personal Home Page :: Article: 303

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.