If you REALLY need to work with these two different encodings, it might be interesting to use these two functions, so as not to rely on connection conversions:
To read from a Latin1 source: and use in UTF-8:
$dados_em_utf8 = utf8_encode( $dados_em_latin1 );
Handbook:
http://php.net/manual/en/function.utf8-encode.php
To read from a UTF-8 source and use in Latin1:
$dados_em_latin1 = utf8_decode( $dados_em_utf8 );
Handbook:
http://php.net/manual/en/function.utf8-decode.php
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 direct way to configure the connection without having to do query with SET
)
Remember that the "REALLY" I commented on in the first line usually implies in one of the two things not to be yours (or the DB or the pages), because if they are, you will hardly have a legitimate reason to mix two encodings.
The best is to convert either the DB or the application, so that they are both in the same encoding.
For this, you need these three steps:
BACKUP (no use crying if you don’t and something goes wrong)
ALTER DATABASE banco CHARACTER SET utf8 COLLATE utf8_general_ci;
This does not change the data, just configure the database for the desired encoding.
ALTER TABLE nomedatabela CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Now you are converting the table DATA to utf8
It might be more interesting utf8_unicode_ci
than the utf8_general_ci
if you want more precision in the compared characters.
More details here:
Doubt with charset=iso-8859-1 and utf8
use the mysqli command, but still the database is in mysql, no ?
– Murilo Melo
Oh yes, I got it wrong. Yes
– LocalHost
So, change it there and put it in mysqli format, and all right
– LocalHost
As soon as I can I will test, whether it works or not, notice here and I will reply, thank you for the readiness :)
– Murilo Melo
Blz. Anything just comment there... I edited there.
– LocalHost
http://answall.com/a/43205/3635
– Guilherme Nascimento