title: pg_query(): Query failed: ERROR: more than one row returned by a subquery used as an expression
body: <p>what's wrong with my query ? im sure its work a few days ago, but today i dont know why its happen. how i can solve this ?
  sorry my bad english</p>
  
  <p>error :
  <a href="https://i.stack.imgur.com/yiX7g.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yiX7g.jpg" alt="enter image description here"></a></p>
  
  <p>query :</p>
  
  <pre><code>function getUserGroupList() {
      $rulesQuery = "SELECT * FROM t_rules";
      $rulesResult = pg_query($this-&gt;DB, $rulesQuery);
  
      $nextQuery = '';
  
      while ($data = pg_fetch_array($rulesResult)) {
          $nextQuery .= ",(SELECT COUNT(*) FROM t_user_group_rules ugp WHERE ugp.ref_user_group = ug.id AND ugp.ref_rules = ".$data['id'].") as ".str_replace(' ', '_', $data['c_name'])."_count,(SELECT c_status FROM t_user_group_rules ugp WHERE ugp.ref_user_group = ug.id AND ugp.ref_rules = ".$data['id'].") as ".str_replace(' ', '_', $data['c_name']);
      }
  
      $query = "SELECT ug.id, ug.c_name $nextQuery
          FROM t_user_group ug
          WHERE ug.c_active = 1";
  
      $result = pg_query($this-&gt;DB, $query);
      // echo $query;
      // exit();
      $all = array();
  
      while ($data = pg_fetch_assoc($result)) {
          array_push($all, $data);
      }
  
      echo json_encode($all);
  }
  </code></pre>
  
  <p>if I echo <code>$query</code>, it will become like this</p>
  
  <pre><code>SELECT
     ug.id,
     ug.c_name,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 1
     ) AS command_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 1
     ) AS command,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 2
     ) AS user_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 2
     ) AS USER,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 3
     ) AS executive_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 3
     ) AS executive,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 4
     ) AS rekon_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 4
     ) AS rekon,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 5
     ) AS ej_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 5
     ) AS ej,
     (SELECT count(*)
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 12
     ) AS tes_count,
     (SELECT c_status
      FROM t_user_group_rules ugp
      WHERE ugp.ref_user_group = ug.id
         AND ugp.ref_rules = 12
     ) AS tes
  FROM t_user_group ug
  WHERE ug.c_active = 1;
  </code></pre>