0

I have a small issue:

I recieve few paramaters via ajax and make a sql query. Simple, but this query has some special characters(brazilian) and my query looks like that(as I've made a echo):

SELECT neigh 
FROM address_bd 
WHERE 
  state ="AL" 
  AND city ="Maceió" 
GROUP BY neigh 
ORDER BY neigh ASC

and in the script is:

$sql = "
  SELECT neigh 
  FROM address_bd 
  WHERE state =\"$state\" 
    AND city =\"$city\" 
  GROUP BY neigh 
  ORDER BY neigh ASC
  ";

Running it in phpmyadmin return the correct result, but in the script gives 0 results. My sql row has utf8_general_ci. Does anyone have any idea? Thank you.

AND here comes the response:

SET character_set_client = utf8;

This resolved the issue. Thank you all and especialy to Inca for sending the link. Thank you again

Gigg
  • 1,019
  • 3
  • 11
  • 20
  • 1
    Have you tried to see what query has been composed finally? – zerkms Sep 11 '11 at 10:50
  • You should also define the character set for the connection `mysql_set_charset('utf8', $connection);` – Shef Sep 11 '11 at 10:58
  • the first one I showed you. I made a echo of the $sql variable and saw that – Gigg Sep 11 '11 at 10:58
  • Most probably $city in your script doesn't contain a utf-8 encoded string, do `$city = utf8_encode($city);` does that help? – nobody Sep 11 '11 at 11:02
  • Have you tried all the steps in http://stackoverflow.com/questions/346531/utf8-problem-with-mysql-5 ? (Make sure your page and connection are also utf8.) Also: utf8_general_ci is a collation option (that has to do with ordering, not with encoding). And depending on where you set `$state` and `$city` your code is likely vulnerable to sql-injection. Consider using prepared statements instead. – Inca Sep 11 '11 at 11:02
  • and mysql_set_charset('utf8', $connection) not working too. The thing is that"Maceió" is a result of a previous query, one made with the where state = "AL". Does it help? – Gigg Sep 11 '11 at 11:11

2 Answers2

1

you should have mysql_set_charset(charset); or corresponding operator for your database driver placed right after your database connect operator.

and charset should represent actual charset of your HTML pages.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Maybe try this:

$sql = "SELECT neigh FROM address_bd WHERE state = '$state' AND city = '$city' GROUP BY neigh ORDER BY neigh ASC";

If it still does not work add this after mysql_query function

print mysql_error();

and tell here the error message content.

Olli
  • 752
  • 6
  • 20