准备好的声明无效 - 空白页

准备好的声明无效 - 空白页

问题描述:

I have this code

$con = new mysqli('####', '####',   '####', '####');
if(mysqli_connect_errno()){
echo 'Connection Failed:' . mysqli_connect_errno();
exit();
}

//Variables

$user = $_POST['username'];
$zone = $_POST['password'];
$pass = strtoupper(hash("whirlpool", $zone));

//Prepare
if($stmt = $con -> prepare("SELECT * FROM `accounts` WHERE Username=? AND Key=?")){






$stmt -> bind_param("ss", $user, $pass);
$stmt -> execute();
$stmt -> bind_results($result);
$stmt -> fetch();
if($result) {

    $_SESSION['username'] = $user;
    $url = 'home.php';
    echo '<META HTTP-EQUIV=Refresh CONTENT="1; URL='.$url.'">';
} else {

    echo 'Login Failed';
}

}
?>

I am new to Prepared statements and I cannot get it to work. Upon trying to log in I just get a blank white page with no error. I know I am connected to the db because if I remove the prepared statement and do it the unsecured way everything logs in just fine.

Please note. I have just been looking up tutorials on prepared statements so I can learn to code more securely. I am in no way a pro with this. Any tips would be greatly appreciated.

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs ewsystem\loginadd.php

That's because you select * (all fields). You should be more specific about the fields you want to get (for example SELECT id FROM ...).
Have a look at examples on PHP doc: 2 fields are selected, 2 parameters for bind_result().

According to @AbrikChakraborty comment you just need add backticks to your field name:

if($stmt = $con -> prepare("SELECT * FROM `accounts` WHERE Username=? AND `Key`=?")){

and according to @caCtus comment:

$stmt -> bind_result($result);

and if you really want to bind unknown number of fields returned you can check this answer or just use PDO.

Verify the actual query, if it fetches the result. I doubt the query itself returns empty result.

"SELECT * FROM `accounts` WHERE Username=$user AND Key=$pass"