encoding php to mysql database

Asked

Viewed 520 times

2

Well, I have a problem with encode when throwing dice on a table mysql for php, my page is with encode utf-8, my bank using latin1, however, I do not know how I do such a conversion, if I change the site’s NCODE, I am obliged to write with the characters of such NCODE, but for the end user it would be horrible to have to use the same, for example, a user would have to type &Atilde to obtain Ã. So I would like to know how I can solve this problem.

2 answers

3

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

2


Well, if you use mysql, you can encode by making the following foma in your database connection file:

  mysqli_query($link,"SET NAMES 'utf8'");
  mysqli_query($link,'SET character_set_connection=utf8');
  mysqli_query($link,'SET charecter_set_client=utf8');
  mysqli_query($link,'SET charecter_set_results=utf8');

And in your HTML, la in the header, put the following metatag:

 <meta charset="utf-8">

That should solve your problems.... I hope I’ve helped!!!

  • use the mysqli command, but still the database is in mysql, no ?

  • 1

    Oh yes, I got it wrong. Yes

  • 1

    So, change it there and put it in mysqli format, and all right

  • As soon as I can I will test, whether it works or not, notice here and I will reply, thank you for the readiness :)

  • Blz. Anything just comment there... I edited there.

  • http://answall.com/a/43205/3635

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.