1
I am trying to make a redirect after giving a message to the user, but the message is not being displayed and let alone the session being destroyed, I do not know exactly what is wrong, the code I have so far is this:
// SEGURANÇA DE ACESSO $sqlUsuario = "SELECT * FROM cadUsuario WHERE IdUsuario = ? AND IdPessoa = ?"; $stm = $conexao->prepare($sqlUsuario); // DEFINE O TIPO DA VARIÁVEL INT OU STR $stm->bindValue(1, $IdUsuario, PDO::PARAM_INT); $stm->bindValue(2, $IdPessoa, PDO::PARAM_INT); $stm->execute(); $sqlUsuario = $stm->fetchAll(PDO::FETCH_OBJ); // CONTAGEM DE REGISTROS RETORNADOS $contUsuarios = count($sqlUsuario); // FECHANDO A CONSULTA $stm->closeCursor(); //SE EXISTIR USUÁRIO MOSTRA CONTEÚDO if ($contUsuarios > 0) { // CÓDIGO DA PÁGINA } else { // SE NÃO EXISTIR USUÁRIO EXIBIR MENSAGEM echo "Erro e redireciona "; sleep(10); // DESTRUINDO A SESSÃO session_start(); session_unset(); session_destroy(); session_write_close(); setcookie(session_name(),'',0,'/'); session_regenerate_id(true); header("Location:index.php"); }
The message is stylized that way:
echo "<div align='center' class='alert alert-danger'>Erro e redireciona </div>";
where this $contUsuarios comes from ?
– Victor Eyer
Hello @VME, I entered the code in the question.
– adventistapr
Try changing $countsUsuarios = Count($sqlUsuario); to $contUsuarios = $stm->rowCount(); and see if something turns up. If not, give an echo $contUsuarios to see which way out
– Victor Eyer
If there are errors, it is not in this question code.
– user60252
To
fetchAll
can returnfalse
. And if you make onecount(false)
the result will be 1. And in php 5 does not generate Warning. So, best would be to use the @VME suggestion– Andrei Coelho