i have this successful query coded inside PHP code, the reason why I put a for loop inside the select query is that ncropfarmingreasonid is dynamic in the sense that future reason name and id will be added so that's why i have to loop the id inside the select statement base on how many rows in the table cropfarmingreason
table cropfarmingreason
table tbl_climatechange SQL:
// to get the number of rows in table cropfarmingreason
$sql = "SELECT * from tbl_cropfarmingreason order by ncropfarmingreasonid";
$query = pg_query($sql);
$count_row = pg_num_rows($query);
$qry_6_12 .= " SELECT count(ncropfarmingreasonid) as counted , " ;
for($i=2;$i<=$count_row;$i++) // loop the number of rows and used $i as ncropfarmingreasonid
{
if(($count_row-$i)==0)
{
$qry_6_12 .= "SUM(CASE WHEN ncropfarmingreasonid = ".$i." THEN 1
ELSE 0 END) a".$i."";
}
else
{
$qry_6_12 .= "SUM(CASE WHEN ncropfarmingreasonid = ".$i." THEN 1
ELSE 0 END) a".$i.",";
}
}
$qry_6_12 .= " FROM tbl_climatechange as c, tbl_household as h, tbl_barangay as b where h.chholdnumber=c.chholdnumber and b.cbrgycode=h.cbrgycode and b.cbrgyname = 'AMPAYON' ";
$query_6_12 = pg_query($qry_6_12);
and this is the result of the query :
I want to know if how can i fetch and assign the value in a variable of every field whithout knowing the no. of fields to be returned since table cropfarmingreason is dynamic. Hope someone could help me :) thanks