Question

I have this query written in php:

<?php
$dsn="sistema";
$usuario="";
$clave="";
/*conexion mediante odbc*/
$cid=odbc_connect($dsn, $usuario, $clave);
if (!$cid){
exit("<strong>Ya ocurrido un error tratando de conectarse con el origen de datos.  </strong>");
}
$sql="SELECT SUM(Document.docimp) AS sumar FROM Document WHERE Doctpo IN ('1','6','7') AND Document.Abocod = 1";
$sql1="SELECT SUM(Document.docimp) AS restar FROM Document WHERE Doctpo IN ('2','3') AND Document.Abocod = 1";
$sql3="Select (A.sumar - A.restar) AS Saldo; 
From (;
Select Document.abocod,;
      (SELECT SUM(Document.docimp) AS sumar;
    FROM Document; 
    WHERE Doctpo IN ('1','6','7'); 
        AND Document.Abocod = 1;
    ),;
    (SELECT SUM(Document.docimp) AS restar ;
    FROM Document; 
    WHERE Doctpo IN ('2','3'); 
        AND Document.Abocod = 1;
    );
From; Document;
Where Document.abocod = 1;
GROUP BY Document.abocod;
)AS A";
$result=odbc_exec($cid,$sql)or die(exit("Error en odbc_exec"));
print odbc_result_all($result,"Border=1"););
?>

The sql takes from Document table, all files of type 1,6 and 7 from Doctpo field, doing a sum of what's in Docimp field, filtering by subscriber number (abocod) equal to 1 and placing the result with it's name (alias).

The sql1 same function as sql, but here is the payment made by customers, using the same fields as above but selecting only type 2 and 3.

sql3 is a query that joins the sql and sql1 in a single query, and executes all the functions that were made separately by sql and sql1.

Queries (SQL) work perfectly when I run them into visual fox pro, but when I execute them from php, sql and sql1 do work, but it always throws errors with the sql3 statement. Saying that table is not recognized or doesn't exist.

I can't figure out how it can't make the join with these tables, if this works perfectly on Visual foxpro itself.

I would like to know if there is a 'cleaner' or 'better' approach with php, odbc and visual fox pro.

Anyone can shed some light on this?

Thanks in advance!

Was it helpful?

Solution

How about just running the query once through instead of two seperate, then joining... Something like

select
      sum( 0000.00 + iif( Doctpo in ( '1', '6', '7' ), Document.docimp, 0 )) as Sumar,
      sum( 0000.00 + iif( Doctpo in ( '2', '3' ), Document.docimp, 0 )) as Restar,
      sum( Document.docimp * iif( Doctpo in ( '1', '6', '7' ), 1, -1 )) as Saldo
   from
      FROM Document; 
   WHERE 
          Document.Abocod = 1;
      AND Doctpo IN ('1','6','7', '2', '3'); 

I'm doing a sum of 0000.00 + IIF() because whatever the first value is returned from a VFP query is the basis of that column type in final set. So, you may need to adjust the default length/precision per your needs if you will expect any balance above 9999.99.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top