Good Night;
I have two tables created in the same database. For both tables, I have two selects. The return of a select would like to concatenate the second select to bring the desired result and save to a variable via PHP.
First select:
$opcoes3 = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$conexao = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER,
PASSWORD, $opcoes3);
$sql3 = "SELECT * FROM receita WHERE datareceita = (SELECT
MAX(datareceita) FROM receita)";
$stm3 = $conexao->prepare($sql3);
$stm3->execute();
while($row3 = $stm3->fetch()) {
$receitatotal = $row3['receita'];
}
The above select brings the result of a value that I set as an example in the database: 11400
The second Select:
<?php
$anomes2 = date('y/m');
$anomes2 = str_replace("/", "", $anomes2);
$opcoes2 = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$conexao2 = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER,
PASSWORD, $opcoes2);
$sql2 = " SELECT (SUM(-valor_boleto)+".$receitatotal.") as total, extract(year_month
from vencimento) as periodo FROM boleto
GROUP BY extract(year_month from vencimento)= '"."20".$anomes2."'";
$stm2 = $conexao2->prepare($sql2);
$stm2->execute();
while($row2 = $stm2->fetch()) {
$soma = $row2['total'];
}
?>
The $ receitatotal I concatenated in the second select to generate a sum, the value of the $ receitatotal is the 11400. But when it sums, it returns a value totally than expected.