How can I put an if in this mysql search when no results return?

Asked

Viewed 76 times

1

how to put an if and Else in this code for when the search in the database does not return any value?

include_once("conexao.php");
$sql= "SELECT FROM tb_nome WHERE nome LIKE '$pesquisar%'";
$salvar = mysqli_query($conexao,$sql);
while ($linhas = mysqli_fetch_array($salvar){
    echo "Nome: ".$linhas['nome']."<br>";
    echo "Nome: ".$linhas['telefone']."<br>";
}
  • 2

    Use mysql_num_rows($salvar), would look something like if(mysql_num_rows($salvar) == 0){ //faça algo se não houver dados }else{/ /faça algo se houver dados }

  • thank you so much for your help

1 answer

1


Try it this way:

if(mysqli_num_rows($salvar) > 0){
    while ($linhas = mysqli_fetch_array($salvar){
        echo "Nome: ".$linhas['nome']."<br>";
        echo "Nome: ".$linhas['telefone']."<br>";
    }
}else{
    echo "Nenhum resultado foi encontrado.";
}
  • 1

    thank you very much friend

  • 1

    I thank you for being able to help, if you can mark the answer I thank you. Any doubts we are there.

Browser other questions tagged

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