MySQL-output is case-sensitive?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

MySQL-output is case-sensitive?

Post by Lureren »

Hello!

Atm. im building a new website with a adminpanel and of course a login-system. But when i enter the username it's in case-sensivity, which i dont want. Eg. "Lureren" works because that is how its entered in the database, while "lureren" doesnt work?

How can i change that?

Im out!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

All the character sets ending with a _ci are case insensitive, so use one of those for the username field.
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: MySQL-output is case-sensitive?

Post by marty pain »

Here you are mate
http://us2.php.net/manual/en/function.strtoupper.php

Convert the user name to upper case before saving it to the database, and again when the user enters their user name to login. Hope that helps!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

but then he can't display the username in the original case the user entered it in, which could be confusing / unwanted behavior.
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

So basicly i just change the characterset in the database?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

correct. better backup the table just in case if you have important data in there
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

Ill give it a try. Thank you ;)
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Re: MySQL-output is case-sensitive?

Post by marty pain »

pytrin wrote:but then he can't display the username in the original case the user entered it in, which could be confusing / unwanted behavior.
True.

If it's not important, then it's an easy fix, but you're right, sorting out the character set is a better solution.
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

Ive now tried som different charactersets, but none of them works.

Any suggestions?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

which ones did you try? you should be using a character set that matches your page encoding, regardless. There shouldn't be that many that match

Also, read the manual - http://dev.mysql.com/doc/refman/5.0/en/ ... ivity.html
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

The webpage is using ISO-8859-1 and in the table ive tried latin1_danish_ci, latin1_swedish_ci, latin1_general_ci, utf8_general_ci and utf8_danish_ci.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

either of the latin ci's should work. What is the query you are running?
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

Code: Select all

$postUsername = $_POST["username"];
$sql = mysql_query("SELECT username,password FROM user WHERE username = '$postUsername'");
$sqlArray = mysql_fetch_array($sql);
 
if($sqlArray["username"] == $_POST["username"] AND $sqlArray["password"] == md5($_POST["password"]))
{
    $_SESSION["logon"] = $postUsername;
}
else
{
    echo "<p>Wrong login!</p>";
}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: MySQL-output is case-sensitive?

Post by Eran »

What you are doing is kind of redundant though I don't think it's related to you error. Since you are retrieving a row by its username, you only have to check that the results are not empty, not compare again the username. You should check that your query produces no errors and that any rows has been retrieved.
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Re: MySQL-output is case-sensitive?

Post by Lureren »

I removed the "== $_POST["username"]" and now it works!

Thanks alot!
Post Reply