There is a code

$s= $data['s']; $s= explode(",",$s); $items = array(); foreach($s $row) { $items = $this->db->query("SELECT * FROM test WHERE id='{$row}'"); } 

With a war dump, items only return 1 item, how can the whole result of a loop be glued into one array?

  • Do not make queries in a loop. Hang up and server and base - Alexey Shimansky

2 answers 2

The array should be filled with [], i.e. instead of $items = , you should write $items[] =

 $s= $data['s']; $s= explode(",",$s); $items = array(); foreach($s $row) { $items[] = $this->db->query("SELECT * FROM test WHERE id='{$row}'"); } 

However, it is better not to execute queries in cycles. The most reasonable thing is to collect $row values ​​into the $items array, and then use the IN keyword

 SELECT * FROM test WHERE id IN (1, 5, 6, 8) 

You can combine the values ​​from $items into a string for IN using the implode() function

     $s = explode(",", $data['s']); $s = array_map('intval', $s); $items = $this->db->query("SELECT * FROM test WHERE id IN(" . implode(', ', $s) . ")");