Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Loop Problem

Posted by Chris_Isaac (Chris Isaac), 29 January 2004
Hi

The bit of script below should after a query to a db, either print "no matches" or print the matches out.  It prints out any matches that are there, but doesn't print "no matches" if none are found, can anyone see anything obvious that is wrong?

Code:
while($row=mysql_fetch_array($rs)) {

           if (count($row)=="") {

                 print"No film by that name exists<br>";
           }
           else{      echo("Film - ".$row["title"]."<br>");
           }
     }


I've also tried changing the if (count($row)=="") to a 0 but that didn't work either.

I did get it close by using the following:

Code:
     while($row=mysql_fetch_array($rs)) {

                 echo("Film - ".$row["title"]."<br>");
           }
     
           if ($row==0) {

                 print"No film by that name exists<br>";
           }



That does print out "no Matches", but also prints "no Matches" to the end of the query if some are found.

Any ideas?

Posted by admin (Graham Ellis), 29 January 2004
If there are no matches, you'll never get into the loop - remember that a while loop runs ZERO or more times, and if there's no match yours runs zero times - i.e. ignores all the code within the loop.

Here's the sort of thing I might write ...

Code:
$matches = 0;
while($row=mysql_fetch_array($rs)) {
    $matches++;
    echo("Film - ".$row["title"]."<br>");
 }
if ($matches < 1) {print "No matches<br>";}


Posted by Chris_Isaac (Chris Isaac), 29 January 2004
Excellent, thats done it, thank you.

Out of curiosity, is there a way of checking if an array is empty and testing against that?  I thought something like if(count($myArray)==0) {.....   would be actioned if there were no elements in the array, but it didn't work, should it have?

Posted by admin (Graham Ellis), 29 January 2004
At first glance, I would have expected that test to work ... if it continues to give trouble, print out the contents of the $myArray variable before the test, and also print something out within the conditional block.   That way, you should be pointed to where to look further for what's not functioning right

Posted by Chris_Isaac (Chris Isaac), 29 January 2004
I thought it would work too, nevermind, got around it now anyway, but thanks for the response.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho