drnpwmq4536 2013-04-29 09:02
浏览 79
已采纳

PHP重复返回相同的结果

When running a while script on my local xampp it just returns the same result over and over again, here's the PHP I'm using:

<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
$getids = mysql_fetch_assoc($getmembers);
    while($id = $getids['userid']){
        $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
        echo ($getusers['username']);
        echo('<br />');
    }
?>

All that I get when I execute that is: http://prntscr.com/12nh6g My database contains 4 different users registered to that group. I'm not sure about joining tables in MySQL as I have no idea how to do it.

If I add a break; then it only returns one result, instead of timing out.

Any help is much appreciated!

Also I'm sorry about how messy the PHP is, I am new to this.

  • 写回答

4条回答 默认 最新

  • duancheng8000 2013-04-29 09:08
    关注
    while($id = $getids['userid']){ #This condition will always be true.
    

    Use your mysql_fetch_assoc statement in a loop like below.

    while ($getids = mysql_fetch_assoc($getmembers)) {
        $id = $getids['userid']
        $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
        echo $getusers['username'];
        echo '<br />';
    }
    

    For sub query you can try,

    SELECT look, username FROM users
    WHERE id IN 
           (SELECT userId FROM group_memberships
            WHERE groupid = $id
            LIMIT 10)
    

    Using JOIN,

    SELECT u.look,u.username
    FROM users as u
    LEFT JOIN group_memberships as gm
         on gm.userId = u.id 
         WHERE gm.groupid = $id
         LIMIT 10 
    

    Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c