Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP while/or?

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#1: Feb 3 '09
I have a loop picking up some data from a MySQL query but if it doesn't find anything I want it to print out something else. Obviously if I use mysql_fetch_array() to test it, my while loop will not pick up the first value so here is what I have:
Expand|Select|Wrap|Line Numbers
  1.     $raw_msgs = mysql_query("SELECT...") or die(mysql_error());
  2.     if (empty($raw_msgs)) { echo("No messages."); }
  3.  
  4.     while ( $msgs = mysql_fetch_array($raw_msgs) ) {
  5.         echo("...");
  6.     }
  7.  
I have tried putting the if statement in the while loop, but very quickly realised how dumb that was because the while loop was not running if it is empty. So is there a while/or function or something which can check if a query is empty without affecting fetching?

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#2: Feb 3 '09

re: PHP while/or?


Expand|Select|Wrap|Line Numbers
  1. if (MySQLi_Result->num_rows === 0) {...}
  2. // or
  3. if (mysql_num_rows($raw_msgs) === 0) {...}
you should read this one: PHP: MySQL Functions
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#3: Feb 3 '09

re: PHP while/or?


Thanks heaps. I have read it before but what is the difference between == and ===?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,661
#4: Feb 3 '09

re: PHP while/or?


the latter checks the type too.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#5: Feb 3 '09

re: PHP while/or?


Ahh yes. Thank you for your help.
Reply