dongwuge6201 2016-02-19 03:14
浏览 84

MySQL php使用另一列获取列(使用用户名获取密码)

After successfully verifying username exists in the database, I need to verify whether the password matches as well. Therefore, I need to retrieve the hashed password from the database, right? Or do I have other choices?

I'm using PHP 5.5 API bcrypt for hashing. My code would give me a 500 internal server error when I get the password from database. How do I do this correctly?

Here's my php code:

// If found username, check password, else respond invalid username
if($countRows!==0){
    // Get hash from database
        // Prepare statement
        $stmt = $conn->prepare('SELECT password FROM users WHERE username = ?');

        // Bind
        $stmt->bind_param('s', $ps_username);

        // Set Parameters
        $ps_username  = $username;

        // Execute
        $hash = $stmt->execute();

        // Check password
        if (!password_verify($password, $hash)) {
            if($error != ""){
                $error .= '<br>';
            }
            $error .= 'The user or password you entered do not match, please try again.';
        }
        else {
          echo 'OK';
            // Session start
            // Redirect user to profile/homepage
        }
}

And can someone recommend something that I can learn SQL commands? I can't find a good place for that.

  • 写回答

1条回答 默认 最新

  • dongpobo6009 2016-02-19 03:24
    关注

    execute() does not return any column data. It returns a boolean (true/false). This is where your code block first fails:

        $hash = $stmt->execute();
    

    You can view examples on how to fetch data from the result set here: http://php.net/manual/en/mysqli-stmt.fetch.php

    An example being:

    $stmt->execute();
    $stmt->bind_result($hash);
    $stmt->fetch();
    

    In response to:

    And can someone recommend something that I can learn SQL commands?

    This is pretty much off topic for Stackoverflow but the PHP manual for mysqli can show you how to use the mysqli API fairly well with plenty of examples. If you want to learn the Structured Query Language itself, then there are plenty of external resources for that, including MySQL's documentation.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制