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 replies on “PHP needs a while-else statement”
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 !
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]';
}
What You think about:
if(!($row=mysql_fetch_assoc($mysqlResult))){
[no record found]
}else do{
[records found]
}while($row=mysql_fetch_assoc($mysqlResult));
thats very true. especially when u want to display mysql result and also an else statement incase there is 0 row found.
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.
I was just hoping for this functionality about 5 minutes ago. There is obvious workarounds, but this would be much much cleaner.
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]
}
A solution (simpler than taaniel’s one) is to put a flag :
$hasresult=false;
while($row=mysql_fetch_assoc($mysqlResult)){
}
There is a feature request to this:
Request #26411 – while {} else {}
There is also a feature request for the corresponding foreach statement:
Request #46240 – Build in foreach else support
Please visit and vote!
No registration or additional details required to vote.
You can subscribe or comment in the ‘Add Comment’ tab by just adding your mail, no registration needed.
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;}
}
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”;
}
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.
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
}
Your method did the trick, thank you so much!
Very nice solution indeed. Thanks. I’ll gladly take this over the proposed while/else for now.
Should there be a semi-colon after “++$n”?
PHP doesn’t support that, but Smarty does.
I normally use
foreach / else to display tables.
+1.
Any PHP developer there?
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.