Ver Mensaje Individual
  #7 (permalink)  
Antiguo 23/05/2013, 12:51
ambichol
 
Fecha de Ingreso: febrero-2013
Ubicación: Lima
Mensajes: 301
Antigüedad: 11 años, 9 meses
Puntos: 5
Respuesta: Concatenar consulta SQL con Variable PHP

Este es el codigo que estoy usando:
Código PHP:
Ver original
  1. function getTopCustomersXML($intYear, $howMany, $forDataURL) {
  2.     // Function to connect to the DB
  3.     $link = connectToDB();
  4.    
  5.     $strSQL = "SELECT top " .$howMany. " c.CompanyName as CustomerName, SUM(d.Quantity*p.UnitPrice) As Total, SUM(d.Quantity) As Quantity FROM Customers as c, [Order Details] as d, Orders as o, Products as p WHERE YEAR(OrderDate)=" . $intYear . " and c.CustomerID=o.CustomerID and o.OrderID=d.OrderID and d.ProductID=p.ProductID GROUP BY c.CompanyName ORDER BY Total DESC";
  6.     //if ($howMany!=-1)
  7.         //$strSQL .= " LIMIT " . $howMany;//no funca en SQL
  8.    
  9.     $result = mssql_query($strSQL) or die(mssql_error());
  10.  
  11.     //Initialize <categories> element
  12.     $strCat = "<categories>";
  13.    
  14.     //Initialize datasets
  15.     $strAmtDS = "<dataset seriesname='Amount'>";
  16.     $strQtyDS = "<dataset seriesName='Quantity' parentYAxis='S'>";
  17.    
  18.     //Iterate through each data row
  19.     if ($result) {
  20.         while($ors = mssql_fetch_array($result)) {
  21.             $strCat .= "<category label='" . escapeXML($ors['CustomerName'],$forDataURL) . "'/>";
  22.             $strAmtDS .= "<set value='" . $ors['Total'] . "'/>";
  23.             $strQtyDS .= "<set value='" . $ors['Quantity'] . "'/>";
  24.         }
  25.     }
  26.     mssql_close($link);
  27.  
  28.     //Closing elements
  29.     $strCat .= "</categories>";
  30.     $strAmtDS .= "</dataset>";
  31.     $strQtyDS .= "</dataset>";
  32.     //Entire XML - concatenation
  33.     $strXML = $strCat . $strAmtDS . $strQtyDS;
  34.        
  35.     return $strXML;
  36. }