Checking if there is a result in the query

Asked

Viewed 74 times

-1

Hello, I need to check if there is an answer to my query. If there is, you should display an HTML code displaying the database data. Otherwise you should notify that there is no result. How do I make this selection?

Example:

$sql = "select * from cronograma WHERE equipe='".$equipe."' AND datas='".$data."' "; 
$cronograma = mysqli_query($conexao, $sql);

//nesse ponto deve verifiar se existe resultado
if($cronograma = existe){
//exibe resultado da tabela}
else{ echo "Não existe resultado";}

1 answer

0

If you want to know if the query got any results greater than 0. You can use the mysqli_num_rows to count the number of lines.

if(mysqli_num_rows($cronograma) > 0){
  // faça algo
}else{ echo "Não existe resultado";}

But if you just want to know if there was success in the connection, just do this:

if($cronograma){
   // faça algo
} else {
  // verifica o erro
  if(mysqli_errno($conexao) == 1064) echo "erro de sintaxe";
}

See the list of mysql error numbers that you can use.

Browser other questions tagged

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