简体   繁体   English

PHP MySQL:在同一张表中选择不同的列的问题

[英]PHP MySQL: issue selecting different columns from the same table

This has surely come up before but I haven't found a solution. 这肯定已经出现过,但是我还没有找到解决方案。 I am trying to select username and password from a database to verify users a simple login script. 我试图从数据库中选择用户名和密码,以验证用户的简单登录脚本。 It should simply find a row in the users table with a username and password matching those submitted through the login form. 它只需在用户表中找到一行,其用户名和密码与通过登录表单提交的用户名和密码相匹配。

I can match the username without any problem but not the password and I have no idea why. 我可以毫无问题地匹配用户名,但是密码却没有问题,我也不知道为什么。

The table contains columns called "username" and "password" and there is only 1 row in the table with a username 'admin' and a password 'testpassword'. 该表包含名为“用户名”和“密码”的列,并且表中只有1行,用户名为“ admin”,密码为“ testpassword”。

Here is the function containing three options - options 1 and 4 work, the other two don't. 这是包含三个选项的函数-选项1和4有效,其他两个无效。 Option 2 is the same as option 1 except it looks up a different column. 选项2与选项1相同,除了它查找不同的列。 I have checked that the column name in the query matches the columns in the table and that the submitted values match. 我检查了查询中的列名是否与表中的列匹配以及所提交的值是否匹配。 I'm not getting any error messages and can't see what might be wrong (something basic, I'm sure...). 我没有收到任何错误消息,也看不到可能出了什么问题(我确定这是基本的东西)。

function new_session ($username, $pw, $inactive) {

    // echo statements verify that variable match database values
    echo "<h2>username = " . $username . "</h2>";
    echo "<h2>password = " . $pw . "</h2>";
    echo "<h2>inactive = " . $inactive . "</h2>";

    $db = mydb::getConnection();

    //option 1
    $statement = $db->prepare('SELECT * FROM users WHERE username = :parameter');
    $statement->bindValue(':parameter', $username);

    //option 2
    //$statement = $db->prepare('SELECT * FROM users WHERE password = :parameter');
    //$statement->bindValue(':parameter', $pw);

    //option 3
    //$statement = $db->prepare('SELECT * FROM users WHERE password = :parameter1 AND username = :parameter2');
    //$statement->bindValue(':parameter1', $username);
    //$statement->bindValue(':parameter2', $pw);

    //option 4
    //$statement = $db->prepare('SELECT * FROM users WHERE username = "admin" AND password = "testpassword"');

    $statement->execute();
    $row = $statement->fetchAll();

    if (count($row) == 1) {
        // SESSION data is set here for options 1 and 4
    } 
}

First thing you need to check is if the passwords in your data base are hashed. 您需要检查的第一件事是数据库中的密码是否已哈希。 They probably should be, and if they are you need to compare using the hashing function PASSWORD 它们可能应该是,如果是,则需要使用哈希函数PASSWORD进行比较

$statement = $db->prepare('SELECT * FROM users WHERE password = PASSWORD(:parameter)');
$statement->bindValue(':parameter', $pw);

Now, if your passwords aren't hashed (shame on you), you might have a different problem. 现在,如果您的密码没有被散列(让您感到羞耻),您可能会遇到其他问题。 As you can see in the above, password is a function name in mysql. 如上所示, password是mysql中的函数名称。 It might be having problems parsing your statement because you are using password as a column name. 由于您使用password作为列名,因此可能无法解析您的语句。 Put tick-marks around the column name password . 在列名password周围tick-marks Like this: 像这样:

$statement = $db->prepare('SELECT * FROM users WHERE `password` = :parameter');
$statement->bindValue(':parameter', $pw);

Notice that those are tick marks , not a single quote. 请注意,这些是tick marks ,而不是单引号。 They are found on the same key that ~ is on, above the tab key. 它们位于Tab键上方所在的同一键上。 These tick marks will indicate that password is a column name. 这些tick marks表示password是列名。

The word "PASSWORD" is a mysql command. 单词“ PASSWORD”是一个mysql命令。 so escape it first like this: 因此,首先像这样逃避它:

 //option 3
    //$statement = $db->prepare('SELECT * FROM users WHERE `password` = :parameter1 AND username = :parameter2')

If this query gives error, then I think you have your password encoded. 如果此查询给出错误,那么我认为您已对密码进行了编码。 Then use this for md5: 然后将其用于md5:

$statement->bindValue(':parameter', md5($pw));

And for sha1: 对于sha1:

$statement->bindValue(':parameter', sha1($pw));

I see no other errors which might could result in no rows :o 我没有看到可能会导致没有行的其他错误:o

感谢所有建议,并抽出宝贵时间研究此问题,我按照建议转义了密码一词,但是我很as愧地说问题是密码表输入的最大长度正在修剪最后一个字符,而我没有发现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM