首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Mysqli预准备语句返回结果

问如何从Mysqli预准备语句返回结果
EN

Stack Overflow用户
提问于 2011-05-01 20:37:59
回答 1查看 2.5K关注 0票数 0

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

代码语言:javascript
复制
$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变量执行以下操作:

代码语言:javascript
复制
while ($row = $result->fetch_assoc()) {

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

}
EN

回答 1

Stack Overflow用户

发布于 2011-05-05 19:43:48

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

看起来你不理解运算符的逻辑--请阅读manual。

尝试以下代码:

代码语言:javascript
复制
//$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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5848386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档