简体   繁体   中英

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. 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

echo $t->group_id;

but it only outputs one number. How do I build an array with the three values from id 999?

Thats because, when you are in the foreach, $t is always updated, and you will get tha last $t only. Put them into an array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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