Marco Gasi
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:
My control panel has this at the top of all pages:
But in the main site pages I get the issue.
This is my site pages head:
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;
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">
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" />
What I'm doing wrong?
try adding this php code:
define('APP_ENCODING', 'UTF-8');
mb_detect_order(APP_ENCODI NG);
mb_http_output(APP_ENCODIN G);
mb_internal_encoding(APP_E NCODING);
define('APP_ENCODING', 'UTF-8');
mb_detect_order(APP_ENCODI
mb_http_output(APP_ENCODIN
mb_internal_encoding(APP_E
ASKER
Hi guys. Tried both but without any effect.
That's really strange, because I don't have this issue in my other websites...
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.
ASKER
Not sure how to do it, Chris, but both CMS and website are on the same server for sure: I installed them me self.
ASKER
Since things work fine between cms and db, am I wrong assuming it is not db issue?
ASKER
This line after the connection solved the issue:
mysqli_query($link, "SET character_set_results=utf8");
Can someone explain why when all the rest failed?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"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 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>);
ASKER
I came back to your first suggestion, Ray, using
mysqli_set_charset($link, "utf8");
This way works but if I usemysqli_set_charset($link, "utf8mb4");
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
ASKER
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).
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html