dounieqi6959 2017-02-08 16:24
浏览 64
已采纳

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

Thanks to anyone who is willing to help. I am trying to run a sql SELECT in a fetch loop of another select loop.

$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();

This code only works if I open another connection in the loop. I know that isnt effective so im trying to see if there is another way. I know there is no variable errors in the code. I would use another form of sql in there is no other option. PS im still a student so im sorry for any obvious answers.

  • 写回答

1条回答 默认 最新

  • dpnw86361 2017-02-08 16:31
    关注

    Use just 1 SQL query, something like this:

    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 = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题