mysqli准备好的声明似乎没有用

时间:2013-07-24 04:50:03

标签: php mysqli statements

我有以下代码:

<?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>

我确信一切都是正确的(没有错误抛出,没有)它只是不起作用! 我试过print_r()来查看返回的对象,我得到了这个: 在execute()之后我执行此操作:

print_r($statement);
die();

它给了我这个:

  

mysqli_stmt对象([affected_rows] =&gt; -1 [insert_id] =&gt; 0   [num_rows] =&gt; 0 [param_count] =&gt; 2 [field_count] =&gt; 1 [errno] =&gt; 0   [错误] =&gt; [error_list] =&gt; Array()[sqlstate] =&gt; 00000 [id] =&gt; 1)

2 个答案:

答案 0 :(得分:0)

如果您查看文档 - http://www.php.net/manual/en/mysqli-stmt.bind-result.php,您会发现您忘记了fetch()数据

$statement->fetch();

答案 1 :(得分:-1)

您可以使用以下代码计算行数而不是$statement->bind_result($numrows);

$statement->store_result();
if ($statement->num_rows == 1)
    echo "yeah correct!<br>";
} else {
    echo "No :(<br>";
}