Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

utf-8 issues

Hi everybody.Even if I set utf-8 as default charset both in php pages and in the db I get characters like 'á'  displayed badly.

This is my table:
CREATE TABLE `contents` (
  `id` int(11) NOT NULL,
  `title` varchar(50) NOT NULL,
  `details` text,
  `album` varchar(250) NOT NULL DEFAULT 'album',
  `picture` varchar(100) DEFAULT '',
  `midnail` varchar(60) NOT NULL DEFAULT '',
  `thumbnail` varchar(60) NOT NULL DEFAULT '',
  `video` varchar(100) DEFAULT '',
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `public` tinyint(4) NOT NULL DEFAULT '0',
  `add_link` tinyint(4) NOT NULL DEFAULT '0',
  `activa` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Open in new window


My control panel has this at the top of all pages:
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Emgie CMS</title>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 

Open in new window

This works fine: in both database and in the control panel pages all characters are displayed correctly: they are insterted and extracted from the database without any issue.
But in the main site pages I get the issue.
This is my site pages head:
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>ATR</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

Open in new window

What I'm doing wrong?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Is the database connection set for UTF8MB4?  Setting the CREATE TABLE CHARSET is one thing, but the character set must be consistent throughout HTML, PHP, MySQL, and all external file system inputs, too.
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
Avatar of Gauthier
Gauthier

try adding this php code:

define('APP_ENCODING', 'UTF-8');
mb_detect_order(APP_ENCODING);
mb_http_output(APP_ENCODING);
mb_internal_encoding(APP_ENCODING);
Avatar of Marco Gasi

ASKER

Hi guys. Tried both but without any effect.
That's really strange, because I don't have this issue in my other websites...
Use your browser to check the encoding of the page. You may find the hosting server is not the same as your web page.
Not sure how to do it, Chris, but both CMS and website are on the same server for sure: I installed them me self.
Since things work fine between cms and db, am I wrong assuming it is not  db issue?
This line after the connection solved the issue:
mysqli_query($link, "SET character_set_results=utf8");

Open in new window

Can someone explain why when all the rest failed?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"SET character_set_results" or "SET NAMES" in MySQL, as well as mysqli_set_charset($link, $charset) function in PHP, are for the connections between PHP (as the client) and MySQL server.  
If your MySQL client is not set to use utf8 as its default connection (or client) charset, you'll have to specify it explicitly when dealing with non-ASCII characters.
You may use one of the following few lines to achieve the same thing:
// if in MySQL
SET NAMES 'charset_name';
SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;

// or if in PHP
mysqli_set_charset($link, $charset) ;
mysqli_query($link, <any of the MySQL lines above>);

Open in new window

I came back to your first suggestion, Ray, using
mysqli_set_charset($link, "utf8");

Open in new window

This way works but if I use
mysqli_set_charset($link, "utf8mb4");

Open in new window

then I have strange chars back again. Reading your article I guess the server version of MySql doesn't support utf8mb4? I got the server version with mysqli_get_server_info and it says 5.5.49-cll-lve
Thank you everybody for your suggestions. At last, I adopted Ray's tecnique, just using 'utf-8' instead of 'utf8mb4' (which for some reason doesn't work for me).