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 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画