puelo
puelo

Reputation: 5977

PHP/MYSQL query no result, encoding wrong?

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.

Upvotes: 2

Views: 242

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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"

Upvotes: 1

Mario Radomanana
Mario Radomanana

Reputation: 1698

Try putting mysql_set_charset after your database connection

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

Upvotes: 0

Related Questions