php encoding to mysql database [duplicate]

2
Well, I'm having a problem with encode when posting data to a table mysql by php , my page is with encode utf-8 , my bank using latin1 , but I do not know how such a conversion, if I change the encode of the site, I am obliged to write with the characters of such encode, 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 get à . So I would like to know how I can resolve this problem.

    
asked by anonymous 14.12.2016 / 02:50

2 answers

2

Well, if you use mysql, you can code by doing the following 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, in the header, put the following meta tag:

 <meta charset="utf-8">

And that's it. This should solve your problems .... I hope I have helped !!!

    
14.12.2016 / 03:02
3

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

    
14.12.2016 / 04:47