我有以下代码:
/* Get the 10 latest posts from users you're following */
$stmt = $cxn->prepare('SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = ?) ORDER BY datetime DESC LIMIT 15');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
/* If a result exists, continue. */
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
/* Get the user's username from their id */
$stmt = $cxn->prepare('SELECT username FROM users WHERE user_id = ?');
$stmt->bind_param('i', $row['user_id']);
$stmt->execute();
$stmt->bind_result($username);
$stmt->fetch();
$stmt->close(); // this is where I'm closing the connection
}
}
在倒数第三行,您会注意到我在 while 循环中关闭了连接。问题是,如果我删除该行,我会在该行得到错误。
致命错误:在非对象上调用成员函数 bind_param()
我猜想关闭连接然后为循环中的下一个元素重新打开它不是一件好事。那么,我该如何解决这个问题?为什么我在移除关闭的连接线时会收到此错误?