I have the following code:

<?php
if(!empty($_POST)){
    $db = new mysqli('localhost', 'root', '', 'verif1');
    $password = $_POST['pass'];
    $username = $_POST['user'];
        $table = "admins";
    $password = hash("sha256", $password);
    $statement = $db->prepare("SELECT COUNT(*) FROM {$table} WHERE username = ? AND password = ?");
    $statement->bind_param("ss", $username, $password);
    $statement->execute();
    $statement->bind_result($numrows);
    if($numrows == 1){
        echo "yeah correct!<br>";
    }else{
        echo "No :(<br>";
    }
}


?>

<form action="" method="POST">
    <input type="textbox" name="user">
    <br>
    <input type="password" name="pass">
        <br>
    <input type="submit">
</form>

I'm sure that everything is correct (no error throwing, nothing) it just doesn't work! I tried print_r() to see the objected that has returned and i get this: after the execute() i do this:

print_r($statement);
die();

and it gives me this:

mysqli_stmt Object ( [affected_rows] => -1 [insert_id] => 0 [num_rows] => 0 [param_count] => 2 [field_count] => 1 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 1 )

有帮助吗?

解决方案

If you check the documentation - http://www.php.net/manual/en/mysqli-stmt.bind-result.php you will see that you've forgotten to fetch() data

$statement->fetch();

其他提示

You can use the following code to count the number of rows instead of $statement->bind_result($numrows);

$statement->store_result();
if ($statement->num_rows == 1)
    echo "yeah correct!<br>";
} else {
    echo "No :(<br>";
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top