dongzouche9108 2014-03-17 03:31
浏览 38
已采纳

取未知没有。 SQL中的字段

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 tbl_cropfarmingreason

table tbl_climatechange enter image description here 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 :

result

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

  • 写回答

2条回答 默认 最新

  • doutongwei4380 2014-03-31 02:08
    关注

    I just used below query to get all values in unknown no. of fields return

    SQL :

                   while ($row_column = pg_fetch_array($query_6_12 )){
                     $col = array();
                        foreach ($row_column as $key=>$value){
                        $col[$key]=$value;
                        }
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂