简体   繁体   English

表中具有多个值的PHP SQL查询ID

[英]PHP SQL query id from table with multiple values in rows

I'm having an issue querying multiple values from rows that have a certain value. 我在从具有特定值的行中查询多个值时遇到问题。 I use the SQL code below to grab column "group_id"'s values if the "id" equals 999. In the "xx_groupmap" table, the id 999 shows up three times with different values. 如果“ id”等于999,我将使用下面的SQL代码获取“ group_id”列的值。在“ xx_groupmap”表中,id 999出现了3次,每次使用不同的值。 Below is my table. 下面是我的桌子。

table xx_groupmap
-------------------
id   |  group_id
-------------------
999  |  2
999  |  7
999  |  8

Below is the code I use 下面是我使用的代码

        $db = JFactory::getDbo();
        $query = "SELECT group_id FROM xx_groupmap WHERE id = '999'";
        $db->setQuery($query);
        $results = $db->loadObjectList();
        foreach ($results as $t) {
        }

So in the end when I want to return the three values of id 999 I use this code 所以最后,当我想返回id 999的三个值时,我使用此代码

echo $t->group_id;

but it only outputs one number. 但只输出一个数字。 How do I build an array with the three values from id 999? 如何使用ID为999的三个值构建一个数组?

Thats because, when you are in the foreach, $t is always updated, and you will get tha last $t only. 那是因为,当您进入foreach时, $t总是会更新,并且您只会得到最后的$t Put them into an array. 将它们放入数组。

$ids = array();
foreach ($results as $t) {
    $ids[] = $t->group_id;
}
var_dump($ids);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM