title: php response is taking too long time
body: <p>I have some problem with getting results from VFP database results... The time which is taking by my php script is veeeery long - about 30 minutes, but it should be much shorter...</p>
  
  <p>So there is the code which is responsible for taking database results:</p>
  
  <pre><code>$connect = odbc_connect("RIS", "", "", SQL_CUR_USE_DRIVER) or die("Error in connection ".  odbc_errormsg($connect));
      $array = array();
      $info = array();
  
      $from = $_GET['from'];
      $to = $_GET['to'];
  
      $rez_nap = odbc_exec($connect, "
                                          select
                                              pacientid as pac_pk,
                                              datanapr,
                                              ocered,
                                              rtgapparat,
                                              doctor as doc_naprav,
                                              pk_na,
                                              data_opis,
                                              arst_opis as id_arst_opis
                                          from
                                              napravlenie
                                          where
                                              not arst_opis == '0000' and
                                              not arst_opis == '' and
                                              datanapr between {^".$from."} and {^".$to."}
                          ") or die(odbc_errormsg());
      while($row_nap = odbc_fetch_array($rez_nap))
      {
          echo "&lt;pre&gt;";
          var_dump($row_nap);
  
          $rez_opis = odbc_exec($connect, "
                                              select
                                                  opis as opisanie,
                                                  doctor as doc_opis
                                              from
                                                  opisanie_rtg
                                              where
                                                  pacientid = '".$row_nap["pac_pk"]."' and
                                                  data = '".$row_nap["data_opis"]."' and
                                                  ocered = '".$row_nap["ocered"]."'
                               ") or die(odbc_errormsg());
          while($row_desc = odbc_fetch_array($rez_opis))
          {
              $op = explode('@#$', $row_desc['opis']);
              $opisanie = iconv( "cp1257", "utf-8", trim( addslashes($op[1]) ) );
              $t_nr_i = substr($op[0], 12);
              $ttt = explode("[", $t_nr_i);
  
              if ( strlen($ttt[0]) &gt; 12 )
              {
                  $tt_nr_is = explode("-", $ttt[0]);
                  $t_nr_is_t = substr($tt_nr_is[0], 0, 8);
                  $t_nr_is_t_2 = substr($tt_nr_is[0], 8, 5);
                  $row_desc["nr_is"] = $t_nr_is_t."-".$t_nr_is_t_2;
              }
              else
              {
                  $t_nr_is_t = substr($ttt[0], 0, 8);
                  $t_nr_is_t_2 = substr($ttt[0], 8, 5);
                  $row_desc["nr_is"] = $t_nr_is_t."-".$t_nr_is_t_2;
              }
              $row_desc['opis'] = $opisanie;
              /////////////////////////////////////////////////////////////////////////////////////////////////////
              $rez_apparat = odbc_exec($connect, "
                                  select
                                      kod,
                                      gruppa,
                                      imy,
                                      prefiks
                                  from
                                      apparatura
                                  where
                                      kod = '".$row_nap["rtgapparat"]."'
                    ") or die("Error in apparatura.dbf - ".  odbc_errormsg($connect));
              while($row_apparat = odbc_fetch_array($rez_apparat))
              {
                  $rez_app_gruppa = odbc_exec($connect, "
                                                      select 
                                                          gruppa,
                                                          prefiks,
                                                          apparati
                                                      from
                                                          apparat_gruppa
                                                      where
                                                          gruppa = '".$row_apparat["gruppa"]."'
                                                   ") or die(error_logs("Error in apparat_gruppa.dbf - ".odbc_errormsg($connect)));
                  while($row_app_gruppa = odbc_fetch_array($rez_app_gruppa))
                  {
                      $rez_personal = odbc_exec($connect, "
                                                          select 
                                                              s_name as doc_opis_name,
                                                              f_name as doc_opis_surname
                                                          from
                                                              personal
                                                          where
                                                              number = '".$row_nap["id_doc_opis"]."'
                                                       ") or die(error_logs("Error in personal.dbf - ".odbc_errormsg($connect)));
                      while($row_personal = odbc_fetch_array($rez_personal))
                      {
                          $row_personal["doc_opis_name"] = iconv("cp1257", "utf-8", trim($row_personal["doc_opis_name"]));
                          $row_personal["doc_opis_surname"] = iconv("cp1257", "utf-8", trim($row_personal["doc_opis_surname"]));
                          $row_personal["filial"] = "r15";
  
                          $info[] = array_merge($row_nap, $row_desc, $row_apparat, $row_app_gruppa, $row_personal);
                      }
                  }
              }
          }
      }
  
      echo json_encode($info);
      odbc_close($connect);
  </code></pre>
  
  <p>the database has about ~80-90k rows with data, and I'm using odbc driver to connect to VFP database, also the database is on the remote server... Any advices what I'm doing wrong? Because I can't figure what I'm doing wrong... Thanks for any advice and help!</p>
  
  <p>P.S Also I need to convert that response into JSON and then save it to MySQL database...</p>
  
  <p><strong>EDIT 1</strong>
  I have the second indexes:
  pacientid, data_opis, ocered into the pacient table, opisanie and napravlenie</p>