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条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。