我现在碰到这样的问题。我现在要做12条执行语句,但是显然一条条做很废时间,时间很久,希望可以得到循环帮助 我的代码如下:       list($tqc)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll="select distinct crew_code,t_name from tro_crew where sel_area='$tqc'";
   $st1=$dbh->prepare("$sqll");
   $st1->execute();
   
   list($tqc1)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll1="select distinct crew_code,t_name from tro_crew where sel_area='$tqc1'";
   $st2=$dbh->prepare("$sqll1");
   $st2->execute();
   
   list($tqc2)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll2="select distinct crew_code,t_name from tro_crew where sel_area='$tqc2'";
   $st3=$dbh->prepare("$sqll2");
   $st3->execute();
   
   list($tqc3)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll3="select distinct crew_code,t_name from tro_crew where sel_area='$tqc3'";
   $st4=$dbh->prepare("$sqll3");
   $st4->execute();
   
   list($tqc4)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll4="select distinct crew_code,t_name from tro_crew where sel_area='$tqc4'";
   $st5=$dbh->prepare("$sqll4");
   $st5->execute();
   
   list($tqc5)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll5="select distinct crew_code,t_name from tro_crew where sel_area='$tqc5'";
   $st6=$dbh->prepare("$sqll5");
   $st6->execute();
   
   list($tqc6)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll6="select distinct crew_code,t_name from tro_crew where sel_area='$tqc6'";
   $st7=$dbh->prepare("$sqll6");
   $st7->execute();
   
   list($tqc7)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll7="select distinct crew_code,t_name from tro_crew where sel_area='$tqc7'";
   $st8=$dbh->prepare("$sqll7");
   $st8->execute();
   
   list($tqc8)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll8="select distinct crew_code,t_name from tro_crew where sel_area='$tqc8'";
   $st9=$dbh->prepare("$sqll8");
   $st9->execute();
   
   list($tqc9)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll9="select distinct crew_code,t_name from tro_crew where sel_area='$tqc9'";
   $st10=$dbh->prepare("$sqll9");
   $st10->execute();
   
   list($tqc10)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll10="select distinct crew_code,t_name from tro_crew where sel_area='$tqc10'";
   $st11=$dbh->prepare("$sqll10");
   $st11->execute();
   
   list($tqc11)=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
   $sqll11="select distinct crew_code,t_name from tro_crew where sel_area='$tqc11'";
   $st12=$dbh->prepare("$sqll11");
   $st12->execute();

解决方案 »

  1.   

    fields 函数是如何定义的?
      

  2.   

    分别定义三组变量数组$tqc,$sqll,$tqc
    用for循环就可以
    for(i=0;i<13;i++)
    {
      list($tqc[i])=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
           $sqll[i]="select distinct crew_code,t_name from tro_crew where sel_area='$tqc[i]'";
           $st[i+1]=$dbh->prepare("$sqll[i]");
           $st[i+1]->execute();
    }
      

  3.   

    哦 应该是for(i=0;i<12;i++)
      

  4.   


      我是这样定义的
    function fields($sql) {
      global $dbh;
      $stmt = $dbh->prepare("$sql");
      if ($stmt->execute()) {
       return  $stmt->fetch();
      }
      

  5.   

    本帖最后由 xuzuning 于 2011-08-10 08:37:48 编辑
      

  6.   

    for(12 次){存入到一个数组里面}
      

  7.   

    select distinct crew_code,t_name, tgc
      from tro_crew t1,
           ( select substr(sel_area,1,3) as tgc from tro_crew where employee_no='39221' ) t2
      where t1.sel_area=t2.tqc
      limit 12;我认为这样写就可以了
      

  8.   

    由于是用ORACLE+PHP写的
    下面是源代码,麻烦各位有空的话可以看下。
    //取出区域 tro_sel
    for($i=0;$i<12;$i++)
    {
      list($tqc[$i])=fields("select substr(sel_area,1,3) from tro_crew where employee_no='39221'");
      $sqll[$i]="select distinct crew_code,t_name from tro_crew where sel_area='$tqc[$i]'";
      $st[$i+1]=$dbh->prepare("$sqll[$i]");
      $st[$i+1]->execute();  echo "1[$sqll[1]]";
        echo "2[$sqll[2]]";
          
    //取出区域 tro_sel 结束echo   " <center> ";
    echo   "<table border=1 width=400 BORDERCOLOR=gray>";
    echo   "<tr>";
    //echo   "<td width=300>作番地址</td>";
    echo   "<td>作番数</td>";
    echo   "<td>保养员</td>";
    //echo   "<td>服务系数</td>";
    //echo   "<td>平均服务系数</td>";
    echo   "</tr></table>";
    echo   " <center> ";
    echo   "<table border=1 width=400 BORDERCOLOR=gray>";
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi' type='text' id='fangshi' size=12/></td>";echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan' onchange='
                 fangshi.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew,$tcname)=$st[$i]->fetch())
           {     
         {
     list($tt)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew'");
                echo "<option value='".$tcrew."' name=qname test='$tt'>$tcrew-$tcname</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";
    //select框的显示结束//1
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi1' type='text' id='fangshi1' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
                echo "<select name='duoxuan1' onchange='
                 fangshi1.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew1,$tcname1)=$st[$i]->fetch())
           {     
         {
     list($tt1)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew1'");
                echo "<option value='".$tcrew1."' name=qname test='$tt1'>$tcrew1-$tcname1</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";
    //2
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi2' type='text' id='fangshi2' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan2' onchange='
                 fangshi2.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew2,$tcname2)=$st[$i]->fetch())
           {     
         {
     list($tt2)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew2'");
                echo "<option value='".$tcrew2."' name=qname2 test='$tt2'>$tcrew2-$tcname2</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";
    //3
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi3' type='text' id='fangshi3' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan3' onchange='
                 fangshi3.value= this.options[this.selectedIndex].test;'>";
     echo "<option value=''></option>";
          while(list($tcrew3,$tcname3)=$st[$i]->fetch())
           {     
         {
     list($tt3)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew3'");
        
                echo "<option value='".$tcrew3."' name=qname3 test='$tt3'>$tcrew3-$tcname3</option>";
    //echo "<option value=''></option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";
    //4
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi4' type='text' id='fangshi4' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan4' onchange='
                 fangshi4.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew4,$tcname4)=$st[$i]->fetch())
           {     
         {
     list($tt4)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew4'");
                echo "<option value='".$tcrew4."' name=qname4 test='$tt4'>$tcrew4-$tcname4</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//5
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi5' type='text' id='fangshi5' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan5' onchange='
                 fangshi5.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew5,$tcname5)=$st[$i]->fetch())
           {     
         {
     list($tt5)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew5'");
                echo "<option value='".$tcrew5."' name=qname5 test='$tt5'>$tcrew5-$tcname5</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//6
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi6' type='text' id='fangshi6' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan6' onchange='
                 fangshi6.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew6,$tcname6)=$st[$i]->fetch())
           {     
         {
     list($tt6)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew6'");
                echo "<option value='".$tcrew6."' name=qname6 test='$tt6'>$tcrew6-$tcname6</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//7
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi7' type='text' id='fangshi7' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan7' onchange='
                 fangshi7.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew7,$tcname7)=$st[$i]->fetch())
           {     
         {
     list($tt7)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew7'");
                echo "<option value='".$tcrew7."' name=qname7 test='$tt7'>$tcrew7-$tcname7</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//8
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi8' type='text' id='fangshi8' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan8' onchange='
                 fangshi8.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew8,$tcname8)=$st[$i]->fetch())
           {     
         {
     list($tt8)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew8'");
                echo "<option value='".$tcrew8."' name=qname8 test='$tt8'>$tcrew8-$tcname8</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//9
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi9' type='text' id='fangshi9' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan9' onchange='
                 fangshi9.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew9,$tcname9)=$st[$i]->fetch())
           {     
         {
     list($tt9)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew9'");
                echo "<option value='".$tcrew9."' name=qname9 test='$tt9'>$tcrew9-$tcname9</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//10
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi10' type='text' id='fangshi10' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan10' onchange='
                 fangshi10.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew10,$tcname10)=$st[$i]->fetch())
           {     
         {
     list($tt10)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew10'");
                echo "<option value='".$tcrew10."' name=qname10 test='$tt10'>$tcrew10-$tcname10</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";//11
    echo   "<tr>";  
    echo   "<td width=105><input name='fangshi11' type='text' id='fangshi11' size=12/></td>";
    echo   "<td width=105>";
    //select框的显示
          echo "<select name='duoxuan11' onchange='
                 fangshi11.value= this.options[this.selectedIndex].test;'>";
      echo "<option value=''></option>";
          while(list($tcrew11,$tcname11)=$st[$i]->fetch())
           {     
         {
     list($tt11)=fields("select count(distinct mfg_no) from pda_mugn_mobile where mmcd='$tcrew11'");
                echo "<option value='".$tcrew11."' name=qname11 test='$tt11'>$tcrew11-$tcname11</option>";
             }
           }
          echo "</select></td>";
    echo "</tr>";echo   "</table>";
    }
      echo "1[$sqll[1]]";
        echo "2[$sqll[2]]";
      

  9.   

    那么多的重复代码。 难道for不能解决嘛!