2

i have a strange problem bugging me for some time now and i am unable to resolve it. For testing purposes i create a URL with a parameter which is used to get a result from the database. It works fine most of the time, but it seems to struggle with special characters, like the "ß".

Here is an example URL where the user is urlencoded:

https://www.testurl.com/login.php?user=bu%DFmann (bu%DFmann = bußmann)

And this is a part of my php script:

$user = new User;
$loginOrEmail = $this->_helper->get_request_value("user", array("get"));
if ($user->check_user_is_valid($loginOrEmail)) {
.........

This is the check_user_is_valid method:

$sql = "SELECT * FROM user WHERE (email='" . mysql_real_escape_string($loginOrEmail) . "' OR login='" . mysql_real_escape_string($loginOrEmail) . "') AND activated = 'yes'";
$result = $database->query($sql);

if (mysql_num_rows($result) < 1) {
    return false;
}
return true;

Now this seems to return false for the url above. If i print the query (with print_r or var_dump) the query looks fine:

SELECT * FROM user WHERE (email='bußmann' OR login='bußmann') AND activated = 'yes'

In fact. If i copy and paste this query to phpMyAdmin and execute it, it yields the row i was expecting. What is going on here? Where is the problem? I am pretty sure it has something to do with wrong encoding or something, because it works for all other names.

I already have mysql_set_charset('utf-8', $connection); after i connect to the database.

Please NOTE: I am aware that this is neither secure nor the best way to go. It is just for testing purposes.

9
  • The syntax is mysql_set_charset('utf8', $connection); and not mysql_set_charset('utf-8', $connection); remove the hyphen and try it again. Commented Feb 19, 2014 at 15:09
  • Oh damn. Mistyped :(. It already is utf8. But i noticed something different. The sql query i get printed out is only correct if my browsers encoding is set to ISO-8859-1. With UTF-8 it looks horribly wrong. Commented Feb 19, 2014 at 15:13
  • Or, after your $user = new User; try $database->query('set name utf8'); or place it before $result = $database->query($sql); see if that makes it kick in. Commented Feb 19, 2014 at 15:14
  • Is your filed "saved as" UTF8? and this in your document <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> Commented Feb 19, 2014 at 15:14
  • And this is what I use in some of mine $con->set_charset("utf8"); $con being my DB connection variable. Which is placed "before" the query SELECT * FROM user... Commented Feb 19, 2014 at 15:20

2 Answers 2

1

In order to close the question and be marked as solved, and as per OP's request including multiple comments to troubleshoot/pinpoint the problem, is to have the file's encoding "saved" as UTF-8 instead of what was presently used "iso-8859-1"

Sign up to request clarification or add additional context in comments.

Comments

0

Try putting mysql_set_charset after your database connection

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.