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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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 - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.