views:

571

answers:

7

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 (e.g. "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.

A: 

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.

Jarod Elliott
There is no error. It just returns 0 rows.
Steven
A: 

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.

Andomar
Nah. Adding utf8_encode() doesn't help.
Steven
Looks like you are right. But I'm not sure how this can be solved. See my post update.
Steven
After opening the connection, execute mysql_set_charset('utf8'). Alternatively, run the SQL queries "SET CHARACTER SET 'utf8'" and "SET NAMES 'utf8'".
Andomar
Yeah, I tried that. But that results in ALL my characters such as æ, ø and å turns into gibberish (when I do normal SELECTs from DB).
Steven
Assuming the gibberish is in the browser, try to inform the browser by adding "<?php header("Content-type: text/html; charset=utf-8"); ?>" to the top of your php file?
Andomar
The header already has that. The problem isn't the unicode on my DB, That is fine. The problem is that mysql_query() adds the  before the ` - thus creating the name 'Church´s'. So the selects searches for 'Church´s', which of cours does not exist in DB.
Steven
Are you using an UTF-8 capable editor to edit your PHP file?
Andomar
Yes. But what unicode my editor is in, would not affect code execution on server.
Steven
Where's the $name coming from? A posted form field, a parameter in the address bar, or somewhere else?
Andomar
First through jQuery - jQuery("input#store_labelName").val(); Then throug PHP - $labelName = $_POST['labelName'];
Steven
I also have a thread here - http://www.phpbuilder.com/board/showthread.php?p=10918471#post10918471 . Maybe that will clarify some issues as well.
Steven
Check the encoding of: the input, the PHP file, the representation of the input in the php file (try combinations of utf8_*code).
streetpc
+4  A: 

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.

Gumbo
My DAL (Data Access Layer) is a class. See last post here: http://stackoverflow.com/questions/975452/need-some-advice-on-error-handling-in-php-mysql.Therefore I must use $this->query to return the value. I'm not able to extend my DAL lass to use either mysqli or PDO (see link) - but I've tested for SQL injection, and so far it can't be done - but I will take proper actions against it.
Steven
Don’t you mean `$this->query_result`? But why do you need that anyway? `$result` has the same value as `$this->query_result`: the return value of `$this->query()`.
Gumbo
If you are refferring to my other post, I've renamed $query_result to $query. And for the above code, the $result was just for testing ( mysql_num_rows($result) ). I've updated above example and removed $result.
Steven
Thanks Gumbo. You just made me realizea huge istake. Now I suddenlt have a variable and function which is called the same. Not god. I'm fixing this. Still, mys main problem is still the same.
Steven
+2  A: 

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%'
Ólafur Waage
That worked! One row was returned and I See tha retrieved name looks like this: Church\u00b4s. So how should I go around fixing the backtick character? Neither addslashes or mysql_real_escape_string escapes the character.
Steven
Use the same character encoding on the client side as the table is created.
nos
It's using the same chr. encoding. Table is UTF8 and I can even try utf8_encode() on the input without any luck.
Steven
Check out this comment. http://is.php.net/manual/en/book.pdo.php#68103
Ólafur Waage
Thanks. But I'm not able to extend my DAL class to either PDO or mysqli. Doing that results in a blank page.
Steven
Sorry it looked like you were using PDO.
Ólafur Waage
I wish. If you see my post update, I have now come down to the core of the issue. $labelName is actually passed as 'Church´s'. utf_encode($labelName) does not help. Alle tables are utf8.
Steven
+2  A: 

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.

Martin Olsen
Curently the log only records connect /disconnect. I'm running MySQL on my Win XP. You know how to get more detailed logging info?
Steven
Just reading the manual. According to http://dotnot.org/blog/archives/2005/01/11/query-logging-in-mysql-on-windows/ there's no problem in logging statements. However, I have no MySQL installations (and even less XPs) so I cannot confirm this.
Martin Olsen
Brilliant Martin. I got the log working and can now see the cause of the problem. The input name = 'Church´s'. The  is a unicode issue I believe. But I don't know what else I can do to have it all in UTF8 - I thought I did have everything in utf8.
Steven
What happens if you try the utf8_decode() function?
Martin Olsen
Thanks for the update Martin. Nothing happends to the input if I decode it. But if I encode it, I get some weird output. Anyways - I'm using htmlentities for now. At least I got it working and will look at this issue later down the road.
Steven
That's probably the wiser choice. Good luck and please let us know how it goes!
Martin Olsen
A: 

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.

Steve
Hi Steven. ';' isn't the problem. I think I have found a solution. Not what I want, but it works. I'lll be using htmlentities for storing values and html_entity_decode when retrieving values.
Steven
*I meant Steve, not Steven :)
Steven
A: 

Are you saving the .php file where you have that query in the same encoding?

Luis