How do I display a message if register found and if not found?

Asked

Viewed 868 times

1

I have the following php routine:

or die ("Não foi possível realizar a consulta ao banco de dados");
while($rowfoto2 = mysql_fetch_array($ridfoto2))
   {

How do I display a message if register found and if not found?

Thank you very much!

  • See the number of rows returned, if zero, echo 'registro não encontrado';

2 answers

7

Basically that’s it:

or die ("Não foi possível realizar a consulta ao banco de dados");
if( mysql_num_rows($ridfoto2) == 0 ) {
    echo 'Não veio nada';
} else {
 while($rowfoto2 = mysql_fetch_array($ridfoto2))
    {

That’s what @rray said in the comments.

Important, since you are developing a new code, use the functions mysqli in place of duties mysql, that are already obsolete and will stop working in the next versions of PHP.

Here is an @rray response that guides this exchange: /a/32822/70

1


A little more optimized but what Bacco answered solves your problem.


or die ("Não foi possível realizar a consulta ao banco de dados");
if (mysqli_num_rows($ridfoto2) > 0) {
    while($row = mysqli_fetch_array($ridfoto2)) {
     //Instruções
    }
} else {
    echo "0 results";
}

  • 2

    Summarizing the comments I left earlier: your code is correct, I just don’t agree that it is an optimization of mine, just a reordering. I understand that if the message comes in if or in the else depends only on the need and/or convenience that the author wants.

Browser other questions tagged

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