1

I have this code that works fine outside of Joomla, but once I put it into a Joomla Module, it does not properly encode. See

"Cura�ao - - converted:Cura?ao"

as an example. This should be

"Cura�ao - - converted:Curaçao"

when encoded properly.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
error_reporting(E_ERROR);
header('Content-Type: text/html; charset=utf-8' );
$includes_url = $_SERVER['DOCUMENT_ROOT'] . "/Joomla33/_Leagues/_includes/";
ini_set('default_charset', 'utf-8');
include_once($includes_url. "php/OpenDB.php");

$query = "SELECT * FROM chasiv_timezones.country ORDER BY country_name ASC";
$result = mysqli_query($conn, $query);   
?>
Country:<select id='country' name='country'>
   <option value=''>-- Select --</option>
   <?php
   while($data = mysqli_fetch_assoc($result)) {
      echo $name = mb_convert_encoding($data['country_name'],'utf8');      
      if($data['country_code'] == $row['country']) { $sel = " selected"; } else { $sel = ""; }
      ?>
      <option value="<?php echo $data['country_code']; ?>" <?php echo $sel; ?>><?php echo $name; ?></option>
      <?php
   }
   ?>   
</select>
<?php
$query = "SELECT * FROM chasiv_timezones.country ORDER BY country_name ASC";
$result = mysqli_query($conn, $query);   
if(!$result) {
   exit("Unable to connect to database to get timezone countries.  SQL:" . $query);
}   

echo "<br />";
mysqli_set_charset($conn, 'utf8mb4');

while($data = mysqli_fetch_assoc($result)) {      
   echo $data['country_name'] . '  - ' . mb_check_encoding($data['country_name'],'utf8');
   if(!mb_check_encoding($data['country_name'],'utf8')) {
      echo "    - converted:" . mb_convert_encoding($data['country_name'],'utf8') . '<Br />';
   } else {
      echo "<br />";
   }
}
?>
1

I found the issue. This line:

mysqli_set_charset($conn, 'utf8mb4');

Needs to be before the mysqli_query and not after.

  • just pay attention to Lodder's comment above and read about using Joomla API to communicate with the database. – FFrewin Apr 21 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.