Php 没有从数据库中获取数据

Php 没有从数据库中获取数据,php,sql,Php,Sql,我正在尝试为一个学校项目开发一个登录系统,但是我遇到了这个错误,我不知道如何修复它 在我的数据库中,密码spy123存储在sha256散列中,但当我尝试在php中获取它时,我没有得到任何回报 当前代码: // checks if the user/password combination works. // $username | String - the username of the user to login // $password | 32byteHash - the hashed p

我正在尝试为一个学校项目开发一个登录系统,但是我遇到了这个错误,我不知道如何修复它

在我的数据库中,密码spy123存储在sha256散列中,但当我尝试在php中获取它时,我没有得到任何回报

当前代码:

// checks if the user/password combination works.
// $username | String - the username of the user to login
// $password | 32byteHash - the hashed password of the user
// return | Boolean - Returns true if the password works for that user.
function forumLib_checkLogin($username, $password){
    $goodLogin = false; //Assume the password is wrong.

    $obtainUserPassword_query = "SELECT password FROM forum_users WHERE username = '$username'";

    $dbc = forumLib_connectToDB();
    $results = mysqli_query($dbc, $obtainUserPassword_query);
    forumLib_closeConnectionToDB($dbc);

    echo "query: $obtainUserPassword_query <br />";
    echo "results: ".mysqli_fetch_array($results)."<br />";
    $row = mysqli_fetch_array($results);
    echo "row[0]: ".$row[0]."<br />";
    echo "password: $password <br />";
    echo "DBpassword: $passwordOnDB <br />";

    //if user exists
    if($results != false){
        //get password from DB
        $row = mysqli_fetch_array($results);
        $passwordOnDB = $row['password'];

        //check password
        if($password == $passwordOnDB){
            //successfull login
            $goodLogin = false;
        }
    }

    return $goodLogin;
}
sql查询没有错,因为当我手动输入时,返回了7D9519224964C72A90B342C565FAB14582D0E974043798F940B471304DB0E24


这真让我困惑。

一旦您第一次调用mysqli\u fetch\u数组,您就已经消耗了第一行数据。随后,当您尝试将其捕获到$row变量中时,该行已被使用,而未被保存:

// Prints  'Array'. Did not save the result
echo "results: ".mysqli_fetch_array($results)."<br />";

// Doesn't get anything because the row has been previously fetched
$row = mysqli_fetch_array($results);

一旦您第一次调用mysqli_fetch_数组,您就已经使用了第一行数据。随后,当您尝试将其捕获到$row变量中时,该行已被使用,而未被保存:

// Prints  'Array'. Did not save the result
echo "results: ".mysqli_fetch_array($results)."<br />";

// Doesn't get anything because the row has been previously fetched
$row = mysqli_fetch_array($results);

改变下面的顺序

echo "query: $obtainUserPassword_query <br />";
echo "results: ".mysqli_fetch_array($results)."<br />";
$row = mysqli_fetch_array($results);
echo "row[0]: ".$row[0]."<br />";
echo "password: $password <br />";
echo "DBpassword: $passwordOnDB <br />";

改变下面的顺序

echo "query: $obtainUserPassword_query <br />";
echo "results: ".mysqli_fetch_array($results)."<br />";
$row = mysqli_fetch_array($results);
echo "row[0]: ".$row[0]."<br />";
echo "password: $password <br />";
echo "DBpassword: $passwordOnDB <br />";

不要使用echo,而是使用print_r$row查看真正从数据库中提取的内容。您还应该测试调用mysqli_查询的返回值,以查看每次执行mysqli_fetch_数组时是否失败,结果集中的光标将递增。此外,查询不会生成输出7D9519224964C72A90B342C565FAB14582D0E974043798F940B471304DB0E24;它是由提供给函数的$password生成的。使用print_r$row查看从数据库中真正提取的内容,而不是echo。您还应该测试调用mysqli_查询的返回值,以查看每次执行mysqli_fetch_数组时是否失败,结果集中的光标将递增。此外,查询不会生成输出7D9519224964C72A90B342C565FAB14582D0E974043798F940B471304DB0E24;它是由提供给函数的$password生成的。啊,让met看看这是否能修复它,然后它就这样做了。我发现了真正的问题是什么//如果$password==$passwordOnDB{//successfull login$goodLogin=false;//Ah让met看看这是否能解决问题。我发现了真正的问题。//如果$password==$passwordOnDB{//successfull login$goodLogin=false,请检查密码//