我现在正在研究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变量执行以下操作:
while ($row = $result->fetch_assoc()) {
$_SESSION['login']['user_id'] = $row['user_id'];
}发布于 2011-05-05 19:43:48
这都是因为$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;
}https://stackoverflow.com/questions/5848386
复制相似问题