感谢所有愿意提供帮助的人。我正在尝试在另一个选择循环的获取循环中运行 sql SELECT。
$mysqli = connect();
$stmt= $mysqli->prepare('SELECT lobby_id, user_penName, lobby_str
FROM `tbl_lobbies`
JOIN tbl_users ON tbl_lobbies.user_id = tbl_users.user_id
WHERE lobby_active = ?');
$stmt->bind_param('i',$on);
$stmt->execute();
$stmt->bind_result($lid, $hostName, $str);
while($stmt->fetch()){
$users=0;
$on = 1;
//echo $lid;
$stmt2= $mysqli->prepare('SELECT `user_id` FROM `tbl_usersInLobbies` WHERE lobby_id = ?');
$stmt2->bind_param('i', $lid);
echo 1;
$stmt2->execute();
$stmt2->bind_result($users);
while($stmt2->fetch()){
echo 1;
$nou .= ' <i class="fa fa-square" aria-hidden="true"></i>';
}
$stmt2->close();
只有当我在循环中打开另一个连接时,此代码才有效。我知道这没有效果,所以我想看看是否还有其他方法。我知道代码中没有变量错误。我会使用另一种形式的 sql,因为没有其他选择。 PS 我还是一名学生,所以对于任何明显的答案我感到很抱歉。
最佳答案
仅使用 1 个 SQL 查询,如下所示:
SELECT tbl_lobbies.lobby_id, tbl_lobbies.user_penName, tbl_lobbies.lobby_str, tbl_usersInLobbies.user_id
FROM `tbl_lobbies`
JOIN tbl_users ON tbl_lobbies.user_id = tbl_users.user_id
LEFT JOIN `tbl_usersInLobbies` ON tbl_usersInLobbies.lobby_id = tbl_lobbies.lobby_id
WHERE lobby_active = ?
关于php - mysql 准备了 fetch 循环,不会使用循环 stmt 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42118455/