Problem with Function

Asked

Viewed 58 times

1

Hello,

I’m trying a problem with my query code in mysql, https://gist.github.com/Alkun/d2239eee1514a0dfc16a

If the user enters some value the search in the database is done normally, but if he leaves the form blank my isset() does not work. The message that appears is:

Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, Boolean Given in C: wamp www bros result.php on line 17

<?php
include ("cabecalho.php");
include ("includes/conecta.php");
include ("includes/func.php");
?>

<?php

//Cria a variavel telefone enviada do formulario na pagina painel.php
$telefone = $_POST['telefone'];

//Verifica se o valor de telefone não é vazio.
if (!isset($telefone)) {
header("location: painel.php");
exit;
}
//Conecta com o banco e seleciona os arquivos para efetuar a contagem.
$con = mysqli_connect("localhost", "root", "", "pizzaria");
$query = mysqli_query($con, "SELECT nome, numero, endereco from clientes where telefone = $telefone");
$count = mysqli_num_rows($query);

//Se a contagem for 0
if ($count == 0) { ?>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
Ops! não encontramos nenhum cliente com este telefone,
<a href="painel.php" class="alert-link">tente novamente!</a>
</div>
<?php
die;
//Se a contagem for 1
} else {
if ($count == 1) { ?>
<div class="alert alert-success">
Encontramos 1 cliente com este telefone!
</div>
<?php
}
}
//Mostra todos os resultados encontrados.
while ($resultado = mysqli_fetch_assoc($query)) {
echo "Cliente: ". $resultado['nome']. "<br/>";
echo "Endereço: ".$resultado['endereco']. "<br/>";
echo "N°: ".$resultado['numero']. "<br/>";
}

?> 
  • Change the isset for empty.

  • thanks for editing the post, I was unable to add in code format! Empty() function worked perfectly, thanks! but because isset() was not working?

1 answer

1

isset checks whether the variable exists and is not null (NULL), while Empty checks if the non-empty value for php the following values are considered empty or false:

"" (string vazia)
0 (0 como inteiro)
0.0 (0 como float)
"0" (0 como string)
NULL
FALSE
array() (um array vazio)
$var; (variável declarada porém sem valor.)

In your code it makes more sense to check if $telefone is not empty than if it exists. Change that line:

if (!isset($telefone)) {

To:

if (!empty($telefone)) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.