PHP needs a while-else statement

I think PHP ought to have a while-else statement. There is, of course, a while statement and an if-else statement, but a while() else statement would be perfect for returning a bunch of rows from a MySQL database:

<?php

$q = mysql_query("SELECT name FROM people WHERE age >= 21");
while (
$person = mysql_fetch_assoc($q))
{
    echo
"$person[name] is at least 21 years old.";
}
else
{
    echo
"No one 21 or older was found in the database!";
}

?>

This hypothetical code queries the database for everyone age 21 or older, looping through all the results. If no results were found, it very conveniently says so. Because there is no while-else statement, the programmer has to nest the the while() loop inside an if() statement. A while-else statement would be much cleaner.

20 thoughts on “PHP needs a while-else statement

  1. Keith
    Also wanted to agree – there should be a whileelse – it just makes sense, that’s how I got here in the first place – I was googling why there wasn’t one !
  2. Keith
    I always count the rows first and if there are no rows then echo “Sory no results found for…blah blah”

    Example:

    $sql=”SELECT * FROM videos WHERE category=’comedy’ “;
    $result=mysql_query($sql);
    $num_rows = mysql_num_rows($result);

    if($num_rows<1){
    echo 'Sorry no videos found in this category';
    }

    // Start looping rows from database.
    while($rows=mysql_fetch_array($result)){

    //results show here
    echo '$rows[videotitle]';
    }

  3. Filip.FFK
    What You think about:

    if(!($row=mysql_fetch_assoc($mysqlResult))){
    [no record found]
    }else do{
    [records found]
    }while($row=mysql_fetch_assoc($mysqlResult));

  4. Sherry
    thats very true. especially when u want to display mysql result and also an else statement incase there is 0 row found.
  5. PHP Beginner
    I know hardly anything about website development and found myself needing a while-else statement. Great that we can Google and that I picked this suggested site. Thanks for the previous comments that suggest simple workarounds.
  6. Brett Alton
    I was just hoping for this functionality about 5 minutes ago. There is obvious workarounds, but this would be much much cleaner.
  7. smarecha
    Woops, wrong move…like i was saying :
    A solution (simpler than taaniel’s one) is to put a flag :

    $hasresult=false;
    while($row=mysql_fetch_assoc($mysqlResult)){
    [do whatever you want]
    $hasresult=true;
    }
    if(!$hasresult){
    [do whatever you want]
    }

  8. smarecha
    A solution (simpler than taaniel’s one) is to put a flag :
    $hasresult=false;
    while($row=mysql_fetch_assoc($mysqlResult)){

    }

  9. hwk
    how do i do it here?

    while(!(connection_aborted() || connection_status() == 1) && $bytes_sent content_length)

    {

    //this sends data

    if(connection_aborted() || connection_status() == 0) {log_write(“Seems the client aborted the file download”); break;}

    }

  10. Jacques
    I wholeheartedly agree, here’s something that I’ve considered using that represents the while-else. The only problem is if you change the list=fetch line, you have to do it twice.

    if (list($a,$b) = mysql_fetch_row($result)) {
    do {
    process($a,$b);
    } while (list($a,$b) = mysql_fetch_row($result));
    } else {
    print “No results returned”;
    }

  11. redcore
    I definitely agree. In fact, I was just trying to just that and upon that not working I googled it and realized it doesn’t exist. It’s a common sense statement, if you ask me (hence why I tried doing it). I reckon a post on php.net’s forum would be a good way of seeing why this cannot be done and if it can, give it some thought and perhaps enough traction to be implimented.
  12. taaniel
    agreed, but alternative way is use one variable and if after while, for example

    $q = mysql_query(‘SELECT * FROM tbl’);
    $n = 0;
    while($r = mysql_fetch_assoc($q))
    {
    // do something
    ++$n
    }
    if(0 == $n)
    {
    // no rows found
    }

    1. Dominor Novus
      Very nice solution indeed. Thanks. I’ll gladly take this over the proposed while/else for now.
  13. Alex Kadis
    Interesting thought, I am inclined to agree with you, however I do not know what would go into making something like this happen, and I have no idea if it is even feasible. Perhaps someone with more knowledge on the subject will chime in.

Comments are closed.