Query sql com PHP Orientado a Objetos retorna vazio.

24/05/2020

0

Prezados do Forum, boa tarde.

estou usando autoload em meu projeto. Abaixo segue o trecho do meu código.

Página home.php este conteúdo:

if (isset($_POST['bx_rc_consultar'])):
$rc_bx = addslashes($_POST['bx_rc']);
if (!empty($rc_bx)):

$bx_ctt = new \App\Model\Baixa_Contrato();
$bx_ctt->SET_BX_RC($rc_bx);
$bx_ctt->SET_BX_ANO($ano);

$rescisao = new \App\Model\Administrativo();
$rescisao->READ_Calcular_Rescisao_Associado($bx_ctt);
endif;
endif;

este é o trecho do "Baixa_Contrato"

<?php

namespace App\Model;

class Baixa_Contrato
{
private $bx_rc, $ano;

public function GET_BX_RC()
{
return $this->bx_rc;
}

public function SET_BX_RC($bx_rc)
{
$this->bx_rc = $bx_rc;
}

public function GET_BX_Ano()
{
return $this->ano;
}

public function SET_BX_Ano($ano)
{
$this->ano = $ano;
}
}
?>

Este é o Administrativo (onde fica a query sql)

namespace App\Model;

class Administrativo
{
public function READ_Calcular_Rescisao_Associado($bx)
{
$sql = "SELECT
COUNT(pagou) as em_atraso,
Concat('R$ ', Replace(Replace(Replace(format(round((valor*COUNT(pagou)),2),2),'.', '|'), ',', '.'), '|', ',')) as tt_atraso,
13-(select count(pagou) from tb_pgto where fk_rc_pgto = ? and pagou = 'Sim' and ano = ?) as em_aberto,
Concat('R$ ', Replace(Replace(Replace(format(round((valor*(13-(select count(pagou) from tb_pgto where fk_rc_pgto = ? and pagou = 'Sim' and ano = ?))),2),2),'.', '|'), ',', '.'), '|', ',')) as tt_aberto,
Concat('R$ ', Replace(Replace(Replace(format(round((valor*(13-(select count(pagou) from tb_pgto where fk_rc_pgto = ? and pagou = 'Sim' and ano = ?)))*0.02,2),2),'.', '|'), ',', '.'), '|', ',')) as multa,
Concat('R$ ', Replace(Replace(Replace(format(round((valor*(13-(select count(pagou) from tb_pgto where fk_rc_pgto = ? and pagou = 'Sim' and ano = ?))+valor*COUNT(pagou)+(valor*(12-(select count(pagou) from tb_pgto where fk_rc_pgto = ? and pagou = 'Sim' and ano = ?))*0.02)),2),2),'.', '|'), ',', '.'), '|', ',')) as tt_rescisao
from tb_pgto where fk_rc_pgto = ? and pagou = 'Não' and ano = ?";


$baixa = Conexao::getConn()->prepare($sql);

$baixa->bindValue(1, $bx->GET_BX_RC());
$baixa->bindValue(2, $bx->GET_BX_Ano());
$baixa->bindValue(3, $bx->GET_BX_RC());
$baixa->bindValue(4, $bx->GET_BX_Ano());
$baixa->bindValue(5, $bx->GET_BX_RC());
$baixa->bindValue(6, $bx->GET_BX_Ano());
$baixa->bindValue(7, $bx->GET_BX_RC());
$baixa->bindValue(8, $bx->GET_BX_Ano());
$baixa->bindValue(9, $bx->GET_BX_RC());
$baixa->bindValue(10, $bx->GET_BX_Ano());
$baixa->bindValue(11, $bx->GET_BX_RC());
$baixa->bindValue(12, $bx->GET_BX_Ano());

$baixa->execute();

if ($baixa->rowCount() > 0):
$resultado = $baixa->fetch(\PDO::FETCH_ASSOC);

return $resultado; else:
return [];
endif;
}
}

objetivo é retornar com a consulta, no entanto retorna vazia, executo a query separadamente no workbench, funciona perfeitamente. não encontro o erro. podem me ajudar?
gratidao a todos.
Leonardo

Leonardo

Responder

Post mais votado

26/05/2020

Olá, amigo. Tudo bem?

Já tentou especificar o tipo de dado de cada parametro, o que pode estar ocorrendo é o php entender que um parametro do sql é texto e voce precisa de um inteiro.

Nesta parte, tente substituir
<?php
$baixa->bindValue(1, $bx->GET_BX_RC());
$baixa->bindValue(2, $bx->GET_BX_Ano());
$baixa->bindValue(3, $bx->GET_BX_RC());
$baixa->bindValue(4, $bx->GET_BX_Ano());
$baixa->bindValue(5, $bx->GET_BX_RC());
$baixa->bindValue(6, $bx->GET_BX_Ano());
$baixa->bindValue(7, $bx->GET_BX_RC());
$baixa->bindValue(8, $bx->GET_BX_Ano());
$baixa->bindValue(9, $bx->GET_BX_RC());
$baixa->bindValue(10, $bx->GET_BX_Ano());
$baixa->bindValue(11, $bx->GET_BX_RC());
$baixa->bindValue(12, $bx->GET_BX_Ano());
?>


por este, adicionando o tipo de dado, sendo "PDO::PARAM_STR" para tipo string e PDO::PARAM_INT para inteiro.
<?php
$baixa->bindValue(1, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(2, $bx->GET_BX_Ano(), PDO::PARAM_INT);
$baixa->bindValue(3, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(4, $bx->GET_BX_Ano(), PDO::PARAM_INT);
$baixa->bindValue(5, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(6, $bx->GET_BX_Ano(), PDO::PARAM_INT);
$baixa->bindValue(7, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(8, $bx->GET_BX_Ano(), PDO::PARAM_INT);
$baixa->bindValue(9, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(10, $bx->GET_BX_Ano(), PDO::PARAM_INT);
$baixa->bindValue(11, $bx->GET_BX_RC(), PDO::PARAM_STR);
$baixa->bindValue(12, $bx->GET_BX_Ano(), PDO::PARAM_INT);
?>


Poste aqui se isto te ajudou.

:D

Alex William

Alex William
Responder

Que tal ter acesso a um e-book gratuito que vai te ajudar muito nesse momento decisivo?

Ver ebook

Recomendado pra quem ainda não iniciou o estudos.

Eu quero
Ver ebook

Recomendado para quem está passando por dificuldades nessa etapa inicial

Eu quero

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar