Charset error, php smarty mysql - php

I've recently come across a weird problem about the meta charset.
If I don't set any charset in my header, all accent like é,è,à.. are show correctly (even var from php) except for text comming from my database are replace by a little question marks in a lozenge.
If I set one of those(I tried both of them) charset in my header
<meta http-equiv="Content-Type" content="text/html" charset="iso-8859-15" />
<meta charset="UTF-8">
Text from my database is okay, but all the rest show the little question marks instead of the accent.
My database character set is UTF-8 unicode, and Collation is UTF_8 general_ci.
Note that I'm using smarty, but I did'nt change the charset in config cause his default is UTF-8.

Okay I found a solution, I'm using an ORM, and i only add the charset=utf8 in the setConnection method like this
$config->set_connections(array(
'development' => 'mysql://user:pass#localhost/mydb;charset=utf8')
);

Related

old site, solving error "The byte stream was erroneous according to the character encoding that was declared"

I need to support an old site. using HTML4,
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >
and DB TABLE fields with latin1_swedish_ci collation.
in PHP code, when connecting to DB, always use set names 'latin1'
Recently, the site host updated to PHP 7.4. Since this update, the France accent letters are not displayed well.
In FireFox console there is the following error:
The byte stream was erroneous according to the character encoding that was declared. The character encoding declaration may be incorrect.
According to cPanel and phpinfo() I now have:
PHP Version 7.4.28
MySQL Version 5.6.41-84.1
default_charset: UTF-8
I guess the error is because I send windows-1252 and receive UTF-8...
I know that the correct solution is to use UTF-8 in the DB and in the meta TAG.
but is there a way for the short run to display all pages in the correct charset?
EDIT: Is there a way to show "latin" encoding when PHP forces me to deliver UTF8? e.g. to disable the PHP forcing, or to convert "latin" strings to UTF8, ...
I started replacing a test TABLE to UTF8, but then realizesed it was not necessary. I can keep using the same DB, but in my site pages I must make some changes:
in the HTML, use <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > instead of <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >
in the PHP, use set names utf8 instead of set names latin1

Accented characters in mySQL table

I have some texts in French (containing accented characters such as "é"), stored in a MySQL table whose collation is utf8_unicode_ci (both the table and the columns), that I want to output on an HTML5 page.
The HTML page charset is UTF-8 (< meta charset="utf-8" />) and the PHP files themselves are encoded as "UTF-8 without BOM" (I use Notepad++ on Windows). I use PHP5 to request the database and generate the HTML.
However, on the output page, the special characters (such as "é") appear garbled and are replaced by "�".
When I browse the database (via phpMyAdmin) those same accented characters display just fine.
What am I missing here?
(Note: changing the page encoding (through Firefox's "web developer" menu) to ISO-8859-1 solves the problem... except for the special characters that appears directly in the PHP files, which become now corrupted. But anyway, I'd rather understand why it doesn't work as UTF-8 than changing the encoding without understanding why it works. ^^;)
I experienced that same problem before, and what I did are the following
1) Use notepad++(can almost adapt on any encoding) or eclipse and be sure in to save or open it in UTF-8 without BOM.
2) set the encoding in PHP header, using header('Content-type: text/html; charset=UTF-8');
3) remove any extra spaces on the start and end of my PHP files.
4) set all my table and columns encoding to utf8mb4_general_ci or utf8mb4_unicode_ci via PhpMyAdmin or any mySQL client you have. A comparison of the two encodings are available here
5) set mysql connection charset to UTF-8 (I use PDO for my database connection )
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
or just execute the SQL queries before fetching any data
6) use a meta tag <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7) use a certain language code for French
<meta http-equiv="Content-language" content="fr" />
8) change the html element lang attribute to the desired language
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
and will be updating this more because I really had a hard time solving this problem before because I was dealing with Japanese characters in my past projects
9) Some fonts are not available in the client PC, you need to use Google fonts to include it on your CSS
10) Don't end your PHP source file with ?>
NOTE:
but if everything I said above doesn't work, try to adjust your encoding depending on the character-set you really want to display, for me I set everything to SHIFT-JIS to display all my japanese characters and it really works fine. But using UFT-8 must be your priority
This works for me
Make your database utf8_general_ci
Save your files in N++ as UTF-8 without BOM
Put $mysqli->query('SET NAMES utf8'); after the connection to the database in your PHP file
Put < meta charset="utf-8" /> in your HTML-s
Works perfect.
If your php.ini default_charset is not set to UTF-8, you need to use a Content-type to define your data. Apply the following header at the top of your file(s) :
header("Content-type: text/html; charset=utf-8");
If you have still troubles with encoding, the cause may be one of the following:
a database server charset problem (check encoding of your server)
a database client charset problem (check encoding of your connection)
a database table charset problem (check encoding of your table)
a php default encoding problem (check default_encoding parameter in parameters.ini)
a multibyte missconfigured (see mb_string parameters in parameters.ini)
a <form> charset problem (check that it is sent as utf-8)
a <html> charset problem (where no enctype is set in your html file)
a Content-encoding: problem (where the wrong encoding is sent by Apache).
SET NAMES worked for me.
My issue was in one of my editing pages the field with the foreign characters would not display, on the production web pages there was no problem.
I know you already have an answer. That's great. But strangely none of these answers solved my issue. I'd like to share my answer for the benefit of the others who may encounter the same issues.
I also had the same problems as the OP, with regards to French accents in a multi-lingual application.
But I encountered this issue for the first time when I had to pass (French accented) data as segments in AJAX calls.
Yes, we must have the database set to work with UTF8. But the fact that AJAX calls had query strings (in my case segments, since I'm using CodeIgniter), I had to simply encode the French text.
To do this on the client-side, use the Javascript encodeURI() function with your data.
And to reverse it in PHP, just use urldecode($MyStr) where data was received as parameters.
Hope this helps.
Type something full French signs in your (php) file
Save that file as UTF-8
Paste line beneath into your website header
header('Content-Type: text/html; charset=utf-8');
Page (file) should look good.
If looks good go here for mysql behavior after (SET_NAMES).

Charset is not working - Spanish characters

This is really driving me crazy. I'm building a spanish website (meaning a lot of latin characters) and I'm using Zend framework.
When I save a á it displays like á . I know is a charset encoding but I dont understand why.
My head charset is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
My database is utf8_general_ci . I've changed the charset to others and always the same problem.
When I look in my database, what is saved is an á
Any idea why is this happening? Thanks!
Here goes the recipe to get rid of encoding headaches:
Your DB, tables and fields must use collation utf8_unicode_ci.
Be sure your code (HTML, PHP... all) is UTF-8 encoded.
Always use this meta tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
This is long:
If you have access to MySQL file configuration my.cnf just add this line:
init-connect='SET NAMES utf8'
This tells MySQL to return results in UTF-8 for each connection.
If you cannot edit my.cnf but you are using PDO, open the connection this way:
$pdo = new PDO($dsn, $usr, $pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
If you aren't using PDO... start using it, what are you waiting for? Meanwhile you can execute this after each connection if you use ext/MySQLi:
$mysql->set_charset('utf8');
Or this if you use plain old ext/MySQL:
mysql_set_charset('utf8', $connection);
The character á and other characters outside the ASCII map can be mapped with unicode.
As you are stroing the data in a table with UTF-8 charset and characters are storing properly.
I would suggest you that
Before you store data
Before you fetch data
Run the query SET NAMES UTF-8. This will fetch your UTF-8 data with taking care of the characters which are outside of ASCII map.
While outputting the data you make sure that you are forcing/directing browser to use the UTF-8 charset to print the data.

CodeIgniter config charset and UTF-8 support

I'm working on a website which uses / stores accented characters in the database. I have the page template set so that the config.php charset variable matches the setting, e.g.:
<meta charset="<?php echo $this->config->item('charset');?>">
The problem I'm having is, when $config['charset'] is set to UTF-8, the form validation fails and it's as if no characters were submitted if an accented character was included. So, for example, a required field will bounce back if á is included anywhere in the string. The string minus the á works fine.
I've managed to get this working by changing the $config['charset'] to ISO-8859-1 and converting text to UTF-8 before inserting / after retrieving from the database with php's utf8_encode() and utf8_decode(). Is this the best way or am I missing something needed in order to get UTF-8, with accented characters, working in CodeIgniter?
Any advice appreciated.
You have to make sure that you use UTF-8 everywhere, and that both PHP and MySQL are configure to handle UTF-8.
In the html, add the meta-tag:
<meta charset="utf-8" />
And save it in UTF-8 format. here is how to do that in notepad++.
Define the MySQL tables to support UTF-8, create table with:
DEFAULT CHARSET=utf8;
And set the connection to:
mysql_set_charset('utf8', $con);
Enable UTF-8 in the php.ini:
default_charset = "utf-8"
For a full manual check Handling Unicode Front To Back In A Web App

Web page text display problem

I keep getting these weird text characters when I display user submitted text. like in the following example below. Is there a way I can fox this using PHP, CSS or something so that the characters are displayed properly?
Here is the problem text.
Problems of �real fonts� on the web. The one line summary:
different browsers and different platforms do �hinting�
Here is my meta tag.
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
It's an encoding problem. Make sure you send the correct encoding to the browser. If it's UTF-8, you'll do it like this:
header("Content-type: text/html; charset=utf-8");
Also, make sure that you store the content using the same encoding throughout the entire system. Set your database tables to utf8. If you're using MySQL, run the SET NAMES utf8 query when connecting to make sure you're running in UTF-8.
These weird characters occur when you suddenly switch encoding.
Also, some functions in PHP take a $charset parameter (e.g. htmlentities()). Make sure you pass the correct charset to that one as well.
To make sure that PHP handles your charset correctly in all cases, you can set the default_charset to utf-8 (either in php.ini or using ini_set()).
Set your page to UTF-8 encoding.
Please check with the char-set in header section.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
use this below one:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
or try this one:
htmlentities($str, ENT_QUOTES);
Could be problem with file encoding please check that your files is correctly encoded, saved as "UTF-8 without boom", also if you are saving to database use SET NAMES UTF-8

Categories