0
I’m making a select for deletar
or inserir
records in my bd
according to the amount of records returned, but the tests I have done are not correct, I do not know exactly how to count the records in a return pdo
.
I’ve used the count
and it didn’t work, I think I’m making a lot of mistakes in my attempt.
I will post the commented code to help in understanding, the code is this:
// VERIFICANDO DIREITOS
$sqlInterface = "
SELECT
gasUsuarioServico.IdUsuario,
gasUsuarioServico.IdServico,
gasServico.IdInterface
FROM
gasUsuarioServico
INNER JOIN gasServico ON (gasUsuarioServico.IdServico = gasServico.IdServico)
WHERE
(gasUsuarioServico.IdUsuario = ?) AND
(gasServico.IdInterface = ?)";
$stm = $pdo->prepare($sqlInterface);
// DEFINE O TIPO DA VARIÁVEL PDO::PARAM_INT OU STR
$stm->bindValue(1, $IdUsuario, PDO::PARAM_INT);
$stm->bindValue(2, $IdInterface, PDO::PARAM_INT);
$stm->execute();
$registros = $stm->fetchAll(PDO::FETCH_ASSOC);
// TENTATIVA DE CONTAGEM DE REGISTROS RETORNADOS
$conSqlDel = count($registros);
if( $conSqlDel == 0 ){
$sqlDel = "DELETE FROM gasUsuarioInterface WHERE gasUsuarioInterface.IdUsuario = ? AND gasUsuarioInterface.IdInterface = ?";
$stm = $pdo->prepare($sqlDel);
$stm->bindValue(1, $IdUsuario, PDO::PARAM_INT);
$stm->bindValue(2, $IdInterface, PDO::PARAM_INT);
$retorno = $stm->execute();
} else {
$sqlInterfaceReg = "
SELECT *
FROM
gasUsuarioInterface
WHERE
(gasUsuarioInterface.IdUsuario = ?) AND
(gasUsuarioInterface.IdInterface = ?)";
$stm = $pdo->prepare($sqlInterfaceReg);
// DEFINE O TIPO DA VARIÁVEL PDO::PARAM_INT OU STR
$stm->bindValue(1, $IdUsuario, PDO::PARAM_INT);
$stm->bindValue(2, $IdInterface, PDO::PARAM_INT);
$stm->execute();
$registros = $stm->fetchAll(PDO::FETCH_ASSOC);
$countInsert = count($registros);
if ($countInsert == 0) {
$arrayDados = array('IdUsuario' => $IdUsuario, 'IdServico' => $IdInterface );
$retorno = $crud->insert($arrayDados);
}
}
In the test I made the variable $conSqlDel return 2 records, but in my bd the value is zero, IE, no record returned.
I’m sending back a JSON, this way:
// MENSAGEM AO USUÁRIO
if ($retorno):
$retorno = array('codigo' => 1, 'mensagem' => print_r($conSqlDel) );
echo json_encode($retorno);
exit();
else:
$retorno = array('codigo' => '0', 'mensagem' => ' Registro não foi atualizado' );
echo json_encode($retorno);
exit();
endif;
I do not know how to check the contents of the variables, being that a simple echo
or print_r
doesn’t work.
of a dump in $conSqlDel and show us the result
– Diego Andrade
It seems to be right, if you give a foreach on the result of select (which gave Count 2 and you say it should be 0) shows something?
– caiocafardo
Hello @Andersoncarloswoss, I already made this test, but with select it did not work, gave a function call error, with Insert, update and delete was quiet, but with select not.
– adventistapr
Yes, rowCount is also an alternative, but it’s not guaranteed. According to the documentation, in some databases it may not work, so the most recommended is the way it is being done.
– Diego Andrade
yes @adventistapr, it returns the affected Rows of the last executed command. However, there is this problem with some databases as quoted.
– Diego Andrade
vc is giving json_encode in wrong variable, put $conSqlDel;
– Diego Andrade