mysql准备了fetch循环,不会使用连接为循环stmt

时间:2017-02-08 16:24:52

标签: php mysql sql

感谢愿意提供帮助的任何人。我试图在另一个选择循环的获取循环中运行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 .= '&nbsp<i class="fa fa-square" aria-hidden="true"></i>';
    }
    $stmt2->close();

此代码仅在我在循环中打开另一个连接时才有效。我知道这不是有效的所以我试图看看是否有另一种方式。我知道代码中没有变量错误。我会使用另一种形式的sql,没有其他选择。 PS我仍然是学生,所以我很抱歉任何明显的答案。

1 个答案:

答案 0 :(得分:0)

只使用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 = ?