If you REALLY need to work with these two different encodings, it might be interesting to use these two functions, not to rely on conversions on the connection:
To read from a Latin1: source and use in UTF-8:
$dados_em_utf8 = utf8_encode( $dados_em_latin1 );
Manual:
link
To read from a UTF-8 source and use it in Latin1:
$dados_em_latin1 = utf8_decode( $dados_em_utf8 );
Manual:
link
If you use mysqli
, it is good to know this function:
$mysqli->set_charset('utf8');
It serves to set the data format of the connection between the server and PHP (and is a more straightforward way to configure the connection without having to with SET
)
Remember that the "REALLY" I commented in the 1st line usually implies that one of the two things is not yours (or the DB or the pages), because if they are, you will hardly have a legitimate reason to mix two encodings.
The best thing is to convert either the DB or the application, so that both are in the same encoding .
For this you need these three steps:
-
BACKUP (it's no use crying if you do not do something)
-
ALTER DATABASE banco CHARACTER SET utf8 COLLATE utf8_general_ci;
This does not change the data, it just sets up the database for the desired encoding.
-
ALTER TABLE nomedatabela CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Now it's converting table DATA to utf8
It may be more interesting to utf8_unicode_ci
than utf8_general_ci
if you want greater accuracy in the compared characters.
More details here:
Doubt with charset = iso-8859-1 and utf8