while 循环中的查询仅显示 1 个结果 [英] query inside while loop only shows 1 result

查看:49
本文介绍了while 循环中的查询仅显示 1 个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 php 中做了一个 while 循环,一切顺利,但问题是我不仅想获得用户的 id 还想获得另一个表中的其他一些东西,所以当我继续在这个 while 循环中进行查询并从第二个表中选择所有内容(其中 id 等于到第一个查询的结果的 id),它只返回 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"];
}
}

提前致谢!请帮帮我!

推荐答案

在没有答案的情况下,我不知道你在幕后使用的是什么 db 框架... PDO、mysqli_ 或(希望不是)mysql_.但是,无论如何,问题可能在于您的第二个查询阻止了第一个查询的继续.我会使用 PDO->fetchAll() 来获取它们......但你说你不能这样做......所以,循环第一个并将这些结果加载到数组中是我要做的第一件事,看看是否这就是问题所在:

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.

这篇关于while 循环中的查询仅显示 1 个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆