如何从Mysqli预处理语句中返回结果

时间:2011-05-01 12:37:59

标签: php mysql mysqli

我正在研究Mysqli。 但是我有一个关于如何从mysqli预处理语句返回结果集的问题。 我在Loginclass.php中有以下代码:

$stmt = mysqli_stmt_init($db->getCon());

    $stmt->prepare('SELECT * FROM `user` WHERE `user_username` = ? AND `user_password` = ?');
        $user_username = $this->_user_name;
        $user_password = $this->_password;
    $stmt->bind_param('ss', $user_username, $user_password);
    $stmt->execute();

    $stmt->store_result();

    if($stmt->num_rows() == 0) {
        return false;
    }

    else {
        $this->setPass(true);
        return $result;
    }

现在我想在login.php中使用返回的$ result var:

执行以下操作
while ($row = $result->fetch_assoc()) {

$_SESSION['login']['user_id'] = $row['user_id'];

}

1 个答案:

答案 0 :(得分:0)

这都是因为$db->getCon()方法名称:)

看起来你不理解运营商的逻辑 - 阅读manual

试试这段代码:

//$stmt is object, which can be returned by prepare method
$stmt = $db->prepare('SELECT * FROM `user` WHERE `user_username` = ? AND `user_password` = ?');
$user_username = $this->_user_name;
$user_password = $this->_password;
$stmt->bind_param('ss', $user_username, $user_password);
$stmt->execute();
$stmt->bind_result($result);  //instead of $stmt->store_result();
$stmt->fetch();

if (empty($result))
{
    return false;
}
else
{
    $this->setPass(true);
    return $result;
}
相关问题