开发者

How to make mysqli_query compare a variable identicly?

Here is my code;

if ($_SERVER['QUERY_STRING']) {
$qrystring = $_SERVER['QUERY_STRING'];
$q = "SELECT * FROM accounts WHERE username = '$qrystring'";
$r = mysqli_query($dbc, $q);
    if ($r) {
        if (mysqli_num_rows($r) > 0) {
            echo 'Yay account active';
        }
        else {
            header('location: create');
        }
    @mysqli_free_result($r);
    }}

Now for example when $qr开发者_StackOverflow中文版ystring = User, and in the database it equals user, mysql still compares it as correct. When I use the identical statement of === it breaks the code and it will not work?

I want it to be case sensitive.

Any ideas?

thanks.


See: http://dev.mysql.com/doc/refman/5.0/en/charset-binary-op.html

SELECT * FROM accounts WHERE BINARY username = '$qrystring'";

And also do what halfdan said! ;)


Please sanitize your $qrystring variable before passing it unfiltered to the database. (See SQL injection).

To make a case sensitive match you will have to use COLLATE on your column:

username = COLLATE latin1_general_cs = '$querystring'

From the manual:

Simple comparison operations (>=, >, =, <, <=, sorting, and grouping) are based on each character's “sort value.” Characters with the same sort value are treated as the same character. For example, if “e” and “é” have the same sort value in a given collation, they compare as equal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜