PHP while/or?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    PHP while/or?

    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_arr ay() to test it, my while loop will not pick up the first value so here is what I have:
    Code:
        $raw_msgs = mysql_query("SELECT...") or die(mysql_error());
        if (empty($raw_msgs)) { echo("No messages."); }
        
        while ( $msgs = mysql_fetch_array($raw_msgs) ) {
            echo("...");
        }
    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
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Code:
    if (MySQLi_Result->num_rows === 0) {...}
    // or
    if (mysql_num_rows($raw_msgs) === 0) {...}
    you should read this one: PHP: MySQL Functions
    Last edited by Dormilich; Feb 3 '09, 11:46 AM. Reason: added MySQLi

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Thanks heaps. I have read it before but what is the difference between == and ===?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the latter checks the type too.

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Ahh yes. Thank you for your help.

          Comment

          Working...