Metay Jack
Metay Jack

Reputation: 53

php mysql query select match exact word

I want to select the match exact word from table, but i got a problem that

if row like : Angel and i did select * from table where row = "angel" it success and first

letter is small and in db its capital,

$r = mysql_fetch_row(mysql_query("SELECT ID 
                                  FROM Login
                                  WHERE Username = 'angel'
                                    And Password = 'zxc'"));
if($r[0])
    die("success");
else
    die("failed");

In mysql Table
Username : varchar(50) : Angel
Password varchar(50) : zxc

results should be falied

because its Angel not angel

so any solution for it

Upvotes: 1

Views: 1960

Answers (6)

esmoreno
esmoreno

Reputation: 666

Try like

SELECT ID
FROM Login
WHERE upper(Username) = upper('angel')

Upvotes: 0

Vishnu Kant Maurya
Vishnu Kant Maurya

Reputation: 15

mysql_query("
    ALTER TABLE `Login`
    CHANGE `Username` `Username` VARCHAR(250) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL
");    

$r = mysql_fetch_row(mysql_query("SELECT ID FROM Login WHERE  Username = 'angel' And Password = 'zxc'"));
if($r[0])
    die("success");
else
    die("failed");

Now it will work.

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this,

$r = mysql_fetch_row(mysql_query("SELECT ID FROM Login WHERE  
            Password = 'zxc' AND Username collate latin1_general_cs = 'angel'"));

Read Case Sensitive Database Query and MySQL case sensitive query

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

Yes this is due to the collation of your table field. you should set it to a case sensitive collation usually suffixed with cs like latin1_swedish_cs

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

You can use BINARY for that.

SELECT *  FROM `table` WHERE BINARY `row` = 'angel'

That will make a case sensitive match.

Upvotes: 2

GautamD31
GautamD31

Reputation: 28763

Try like

SELECT ID
FROM Login
WHERE Username = 'angel' COLLATE utf8_bin;

Upvotes: 0

Related Questions