AMIGOS tengo una base de datos que almacena los datos de venta de 2 empresas
al momento de querer sacar el calculo de rentabilidad se demora muchisimo en enviar los resultados y entiendo es por la complejidad de la base de datos.
Bueno ahora bien, en mi sistema que utilizo para hacer las consultas (EMS)
salen los resultados perfectamente, pero al momento de hacer la consulta en php
no me envia los resultados. 
la consulta es la siguiente    
Código SQL:
Ver originalSELECT 
  persona.id_persona,
  CONCAT(persona.nombrepersona, ' ', persona.apellidopersona) AS VENDEDOR,
  ROUND(IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario)))) AS FINAL,
  ROUND(SUM( registroarticulos.costoarticulo*(detalledocumento.cantidad - IFNULL(detalledocumento1.cantidad,0))))AS COSTO,
  ROUND(( IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario)))-SUM( registroarticulos.costoarticulo*(detalledocumento.cantidad - IFNULL(detalledocumento1.cantidad,0))))) AS UTILIDAD,
  ROUND((( IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario)))-SUM( registroarticulos.costoarticulo*(detalledocumento.cantidad - IFNULL(detalledocumento1.cantidad,0))))/IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario))))*100,1) AS PORCENTAJE
 
FROM
  persona
  INNER JOIN documento ON (persona.id_persona = documento.fkcreador)
  INNER JOIN detalledocumento ON (documento.iddocumento = detalledocumento.fkiddoc)
  LEFT OUTER JOIN documento documento1 ON (documento.iddocumento = documento1.numerodocobedece)
  LEFT OUTER JOIN detalledocumento detalledocumento1 ON (documento1.iddocumento = detalledocumento1.fkiddoc)
  AND (detalledocumento1.fkcodigoarticulo = detalledocumento.fkcodigoarticulo)
  INNER JOIN registroarticulos ON (detalledocumento.fkcodigoarticulo = registroarticulos.fkcodigoarticulo)
WHERE
  documento.fkidvendedor = 126 AND 
  documento.fkidestadodoc = 113 AND 
  (documento.fkidtipodoc = 113 OR documento.fkidtipodoc=115 )AND 
  documento.fechaemisiondoc BETWEEN STR_TO_DATE(CONCAT('01-12-2010', ' 00:00:00'), '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE(CONCAT('31-12-2010', ' 23:59:59'), '%d-%m-%Y %H:%i:%s') AND 
  registroarticulos.idregistroarticulo=(SELECT MAX(registroarticulos.idregistroarticulo) 
                                        FROM registroarticulos
                                        WHERE registroarticulos.fkcodigoarticulo = detalledocumento.fkcodigoarticulo )
GROUP BY
  persona.id_persona,
  CONCAT(persona.nombrepersona, ' ', persona.apellidopersona)
  
ahora bien si me resulta en el  EMS (programa para hacer consultas)
por que no me resulta en php?
siendo este codigo el de php:   
Código PHP:
Ver originalif(!$_SESSION['rut'])
{
    include('../includes/php/funciones.php');
    salidasistema3();
}
include('../includes/php/coneccion.php');
    header('Content-type: application/vnd.ms-excel');     header("Content-Disposition: attachment; filename=RUTA".$_GET['fecha'].".xls"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cierre</title>
</head>
<body>
<div id="contenedor_principal" align="center"  style="width:99%; height:99%; display:block; font-size:18px">
 
    <div style="height:10%; width:99%; font-size:18px;"></div>
    
    
    <div style="height:89%; width:99%; font-size:18px;">
 
<?php
    
//  if(!empty($_GET['e_credito']) )
//  {   
        $link = conectar2();
 
        /* execute multi query */
    
  
                        $fecha=$_GET['fecha'];
                  
                
                        $xsql="SELECT 
  persona.id_persona,
  CONCAT(persona.nombrepersona, ' ', persona.apellidopersona) AS VENDEDOR,
  ROUND(IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario)))) AS VENDIDO,
  ROUND(SUM( registroarticulos.costoarticulo*(detalledocumento.cantidad - IFNULL(detalledocumento1.cantidad,0))))AS COSTO,
  ROUND(( IFNULL((SUM(detalledocumento.cantidad * detalledocumento.vunitario) - SUM(detalledocumento1.cantidad * detalledocumento1.vunitario)),(SUM(detalledocumento.cantidad * detalledocumento.vunitario)))-SUM( registroarticulos.costoarticulo*(detalledocumento.cantidad - IFNULL(detalledocumento1.cantidad,0))))) AS UTILIDAD
FROM
  persona
  INNER JOIN documento ON (persona.id_persona = documento.fkcreador)
  INNER JOIN detalledocumento ON (documento.iddocumento = detalledocumento.fkiddoc)
  LEFT OUTER JOIN documento documento1 ON (documento.iddocumento = documento1.numerodocobedece)
  LEFT OUTER JOIN detalledocumento detalledocumento1 ON (documento1.iddocumento = detalledocumento1.fkiddoc)
  AND (detalledocumento1.fkcodigoarticulo = detalledocumento.fkcodigoarticulo)
  INNER JOIN registroarticulos ON (detalledocumento.fkcodigoarticulo = registroarticulos.fkcodigoarticulo)
WHERE
  documento.fkidvendedor = 126 AND 
  documento.fkidestadodoc = 113 AND 
  (documento.fkidtipodoc = 113 OR documento.fkidtipodoc=115 )AND 
  documento.fechaemisiondoc BETWEEN STR_TO_DATE(CONCAT('01-12-2010', ' 00:00:00'), '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE(CONCAT('31-12-2010', ' 23:59:59'), '%d-%m-%Y %H:%i:%s') AND 
  registroarticulos.idregistroarticulo=(SELECT MAX(registroarticulos.idregistroarticulo) 
                                        FROM registroarticulos
                                        WHERE registroarticulos.fkcodigoarticulo = detalledocumento.fkcodigoarticulo )
GROUP BY
  persona.id_persona,
  CONCAT(persona.nombrepersona, ' ', persona.apellidopersona)";
                        
                
        echo '<table width="500" height="34" border="1" cellpadding="1" cellspacing="1">
  <tr>
   
    <td>ID</td>
    <td>Vendedor</td>
    <td>Vendido</td>
    <td>Costo</td>
    <td>Utilidad</td>
    <td>Porcentaje</td>
 
  </tr>
        ';
        
        {
            do {
                /* store first result set */
                
                {
                    {
                        
                        //printf("%s\n", $row[0]);
                        ?>
                         <tr>
                        
                            <td style="font-size:18px; width:50px"><?php echo $row[1]; ?></td>
                            <td style="font-size:10px;  width:50px">
<?php echo htmlentities($row[2]); ?></td>
                            <td style="font-size:18px;  width:50px"><?php echo $row[3]; ?></td>
                            <td style="font-size:10px;  width:50px">
<?php echo htmlentities($row[4]); ?></td>
                             <td style="font-size:18px;"><?php echo $row[5]; ?></td>
                              <td style="font-size:18px;"><?php echo $row[6]; ?></td>
                                <td style="font-size:18px;"></td>
                            
                           
      </tr>
                        <?php
                        $cobrar=$cobrar+$row[4];
                        $pagado=$pagado+$row[5];
                        $saldo=$saldo+$row[6];
                    }
                }
                /* print divider */
        //      if (mysqli_more_results($link)) 
        //      {
                    //printf("-----------------\n");
        ///     }
        }       
        
        echo '</table><table width="500" height="34" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
 
  </tr>
 
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
 
  </tr>
  <tr>
 
    <td></td>
    <td></td>
    <td style="border-top:thin; border-color:#000">Conforme Caja</td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
 </table>';       
                  
                  
                  
 
      
      
      
      
               
    
    
    
    
    ?>
 
 
<table><tr><td style="border-top:thin; border-color:#000"></table>
 
</div></div>
 
 
 
</body>
</html>