简体   繁体   中英

Encoding problems in PHP / MySQL

EDIT: After feedback from my original post, I've change the text to clarify my problem.

I have the following query (pseudo code):

$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_query("SET NAMES 'utf8'; COLLATE='utf8_danish_ci';");

mysql_query("SELECT id FROM myTable WHERE name = 'Fióre`s måløye'", $conn);

This returns 0 rows.

In my logfile, I see this:

255 Connect     root@localhost on 
255 Query       SET NAMES 'utf8'; COLLATE='utf8_danish_ci'
255 Init DB     norwegianfashion
255 Query       SELECT id FROM myTable WHERE name = 'Fióre`s måløye'
255 Quit
  • If I run the query directly in phpMyAdmin, I get the result.
  • Table encoding: UTF-8
  • HTML page encoding: UTF-8
  • I can add records (from form input) where names uses accents (eg "Fióre`s Häßelberg")
  • I can read records with accents when using -> "name LIKE '$labelName%'"
  • The information in the DB looks fine

I have no clue why I can't select any rows which name has accent characters.

I really hope someone can help me.

UPDATE 1: I've come to a compromise. I'll be converting accents with htmlentities when storing data, and html_entity_decode when retrieving data from the DB. That seems to work.

The only drawback I see so far, is that I can't read the names in cleartext using phpMySQL.

I think you should rather return $result than $this->query .

Additionally you should be aware of SQL injection and consider using mysql_real_escape_string or Prepared Statements to protect you against such attacks. addslashes is not a proper protection.

Try this query. If you get results, then it's an issue with your backtick character in the query

SELECT * FROM sl_label WHERE name Like 'Church%'

As other answers indicate, this very much seems like an encoding problem. I suggest turning on query logging ( http://dev.mysql.com/doc/refman/5.1/en/query-log.html ) as it can show you what the database really receives.

UPDATE : I finally found a page explaining the dirty details of PHP and UTF-8 ( http://www.phpwact.org/php/i18n/charsets ). Also, make sure you read this ( http://niwo.mnsys.org/saved/~flavell/charset/form-i18n.html ) to understand how you to get proper data returned from form posts.

Maybe try checking for error messages after calling the query (if you aren't already doing this outside that function). It could be telling you exactly what's wrong.

As Artem commented, printing out the actual query is a good idea - sometimes things aren't exactly as you expect them to be.

This might be an encoding issue, the ' in Church's might be a fancy character. PHPMyAdmin could be UTF-8, and your own PHP website could be iso-latin1.

I'm looking at this line

mysql_query("SET NAMES 'utf8'; COLLATE='utf8_danish_ci';");

and I think it might be an error. With the ';' you are sending two queries to the server, but COLLATE is a clause, not a legal statement on its own. Try:

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_danish_ci'");

If the COLLATE clause is not being accepted by the server, you might be having the problem of your label column having a danish_ci collation, but the statements coming in have the default (prob utf_general_ci). There would be no match for the accented characters, but the wildcard works because the representation for the basic ascii characters are the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM