PHP / MySQL with coding problems

I'm having trouble with PHP regarding coding.

I have an HTML / jQuery HTML5 JavaScript page that interacts with my PHP script using $ .post. However, PHP is facing a strange problem, probably related to the encoding.

When I write

htmlentities("í") 

I expect PHP to output í . However, instead, it prints í At first I thought I was wrong in the encodings, however

 htmlentities("í")=="í"?"Good":"Fail"; 

displays "fail" where

 htmlentities("í")=="í"?"Good":"Fail"; 

But htmlentities($search, null, "utf-8") works as expected.

I want PHP to communicate with the MySQL server, but it also has encoding problems, even if I use utf8_encode. What should I do?

EDIT: In SQL command write

 SELECT id,uid,type,value FROM users,profile WHERE uid=id AND type='name' AND value='XXX'; 

where XXX contains no characters, it works as expected, but it is not, if there is any 'í' char.

 SET NAMES 'utf8'; SET CHARACTER SET 'utf8'; SELECT id,uid,type,value FROM users,profile WHERE uid=id AND type='name' AND value='XXX'; 

Not only fails for characters, but it also doesn't work for strings without any special characters. Removing "characters from SET NAMES and SET CHARACTER SET" does not seem to change anything.

I am connecting to a MySQL database using PDO.

EDIT 2: I am using MySQL version 5.1.30 XAMPP for Linux.

EDIT 3: Launch SHOW VARIABLES LIKE '%character%' from PhpMyAdmin Outputs

 character_set_client utf8 character_set_connection utf8 character_set_database latin1 character_set_filesystem binary character_set_results utf8 character_set_server latin1 character_set_system utf8 character_sets_dir /opt/lampp/share/mysql/charsets/ 

Running the same request from my PHP script output (with print_r):

 Array ( [0] => Array ( [Variable_name] => character_set_client [0] => character_set_client [Value] => latin1 [1] => latin1 ) [1] => Array ( [Variable_name] => character_set_connection [0] => character_set_connection [Value] => latin1 [1] => latin1 ) [2] => Array ( [Variable_name] => character_set_database [0] => character_set_database [Value] => latin1 [1] => latin1 ) [3] => Array ( [Variable_name] => character_set_filesystem [0] => character_set_filesystem [Value] => binary [1] => binary ) [4] => Array ( [Variable_name] => character_set_results [0] => character_set_results [Value] => latin1 [1] => latin1 ) [5] => Array ( [Variable_name] => character_set_server [0] => character_set_server [Value] => latin1 [1] => latin1 ) [6] => Array ( [Variable_name] => character_set_system [0] => character_set_system [Value] => utf8 [1] => utf8 ) [7] => Array ( [Variable_name] => character_sets_dir [0] => character_sets_dir [Value] => /opt/lampp/share/mysql/charsets/ [1] => /opt/lampp/share/mysql/charsets/ ) ) 

Launch

 SET NAMES 'utf8'; SET CHARACTER SET 'utf8'; SHOW VARIABLES LIKE '%character%' 

prints an empty array.

+4
php mysql encoding utf-8
Jan 01 '09 at 23:35
source share
3 answers

It is very important to specify the htmlentities encoding so that it matches the input encoding, as it was in your last example, but omitted in the first three.

 htmlentities($text,ENT_COMPAT,'utf-8'); 

Regarding communication with MySQL, you need to make sure that the connection mapping and character set match the data you pass. You can either install this in the configuration file or at runtime using the following queries:

 SET NAMES utf8; SET CHARACTER SET utf8; 

Verify that the character set for the table, database, and server match. There is one setting that you cannot change at run time, and the server character set. You need to change it in the configuration file:

 [mysqld] character-set-server = utf8 default-character-set = utf8 skip-character-set-client-handshake 

For more information on character sets and mappings in MySQL, see the manual .

+17
Jan 01 '09 at 23:41
source share

Late revival. But for further reference, here are some additional tips:

  • Use mysql_set_charset instead of SET xxx
  • Make sure you save the file with UTF-8 encoding (this is often skipped)
  • Set headers:
    <?php header("Content-type: text/html; charset=utf-8"); ?>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  • If your Apache server configuration contains the AddDefaultCharset directive with a different encoding, contact your host administrator.
+7
Aug 23 2018-11-11T00:
source share

I just ran into this problem. I have all the website content in Spanish, with all the special characters you can expect (as before) and their capitalized versions.

In my case, it was an inconsistency with the encoding / mapping of the server. Everything else was installed on utf8, but on the charset server that had latin1. This led to the fact that all utf8 data entered into the database was displayed in its raw encoded form, for example, L í would equal A with the tilde ~ ...

I use mysqli, and to fix this, I used the method described above by Anthony Axoli (using mysql_set_charset). This method has mysqli , and this is what I used.

After that, I was puzzled. I still had a mess while browsing my website. Of course, I did not know that by changing this latin1 to utf8, I also messed up the encoding / decoding of all this. So I used the help of an online row encoder / decoder to correct my table data.

I made various export data of all my content data (you can configure it to receive update requests, and it will be faster for the update process) and run SQL output through the aforementioned online encoder / decoder, and then copy the paste fixed requests in the phpmyadmin panel sql ... thus correcting coding errors. Now everything is as it should be, And again I can process searches with losses: Maria, Mary, Mary, Mary will correspond to Mary, Mary, Mary, etc. All sharp characters evaluate their basic vowel character. Epic victory.

+1
Dec 13 2018-12-12T00:
source share



All Articles