为什么我的脚本没有从mySQL返回记录?

时间:2017-05-15 13:05:52

标签: php mysql

我正在使用mySQL和PHP构建登录门户

我有这个文件(dbc.php):

<?php

class db_connect {
protected $DB_SERVER = "localhost";
protected $DB_USERNAME = "root";
protected $DB_PASSWORD = "";
protected $DB_DATABASE = "mydb";

public function connect() {
    $conn = new mysqli($this->DB_SERVER, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_DATABASE);
    if(mysqli_connect_errno()) {
        die("Connection failed: ". mysqli_connect_errno());
    }
    return $conn;
}
}
?>

然后我的实际PHP脚本(login.php)从登录页面接受POST:

<?php

//include database connection
include("dbc.php");
session_start();

//put post values into variables
$username = $_POST['username'];
$password = $_POST['password'];

//create db connector object
$db = new db_connect();
$conn = $db->connect();

//select correct db
mysqli_select_db($conn,”mydb”);

$username = mysqli_real_escape_string($conn,$username);

$query = "SELECT password FROM mydb.users WHERE username = '$username'";

$result = mysqli_query($conn,$query);

if(mysqli_num_rows($result) == 0)
{
header('Location: sorry.html');
}


$pwhash = $result;

if (password_verify($password, $pwhash)) {
header('Location: welcome.php');
} else {
header('Location: sorry.html');
}
?>

这永远不会返回奇数值。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

$result包含MySQLi响应资源,而不是字符串或数组。

您需要更改此行:

$pwhash = $result;

对此:

$pwhash = mysqli_fetch_assoc($result)['password'];