douke1954 2015-04-29 17:09
浏览 52
已采纳

查询内部while循环只显示1个结果

i'm making a while loop in php and it all goes well but the problem is that I don't only want to get the id of the user but also some other stuff that is inside another table, so when I go ahead and make a query inside this while loop and select everything from that second table (where the id is equal to the id of the result from the first query), it only returns 1 result...

So this is the code that I currently have:

public function getFriends($id)
{
global $params;

$get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
                          array(
                                "{$this->DB['data']['friends']['one']}" => $id
                          )
                        );

if($get)
{
    while($key = $get->fetch())
    {
        $query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
                                   WHERE {$this->DB['data']['users']['id']} = :id",
                                   array(
                                         "id" => $key->{$this->DB['data']['friends']['two']}
                                   )
                                 );

        while($row = $query->fetch())
        {
            $params["user_friends"][] = [
                "id"   => $key->{$this->DB['data']['friends']['two']},
                "name" => $row->{$this->DB['data']['users']['username']},
                "look" => $row->{$this->DB['data']['users']['figure']}
            ];
        }
    }
}
else
{
    $params["update_error"] = $params["lang_no_friends"];
}
}

Thanks in advance! Please help me out!

  • 写回答

2条回答 默认 最新

  • dongmingxiang0312 2015-04-29 17:47
    关注

    In the absence of answers, I don't know what db framework you are using behind the scenese...PDO, mysqli_, or (hopefully not) mysql_. But, in any case, the problem might be that your second query stops the first from continuing. I would use PDO->fetchAll() to get them all...but you say you can't do that...so, looping the first and loading those results into an array is the first thing I would do to see if this is the problem:

    public function getFriends($id)
    {
        global $params;
    
        $get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
                                  array(
                                        "{$this->DB['data']['friends']['one']}" => $id
                                  )
                                );
    
        $firstResults = array();
        if( $get ) {
            while( $key = $get->fetch() ) {
                $firstResults[] = $key;
            }
        }
        else
        {
            $params["update_error"] = $params["lang_no_friends"];
        }
    
        foreach( $firstResults AS $key )
        {
            $query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
                                       WHERE {$this->DB['data']['users']['id']} = :id",
                                       array(
                                             "id" => $key->{$this->DB['data']['friends']['two']}
                                       )
                                     );
    
            while($row = $query->fetch())
            {
                $params["user_friends"][] = [
                    "id"   => $key->{$this->DB['data']['friends']['two']},
                    "name" => $row->{$this->DB['data']['users']['username']},
                    "look" => $row->{$this->DB['data']['users']['figure']}
                ];
            }
        }
    }
    

    If this doesn't work, then we need more data...e.g. what is the query generated? When you run it manually does it return more than one result? If you get rid of the inner-query, does this fix it? etc.

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证