0

How to exit from if condition not in while loop ?.when country is match to stored country at that time exit from the if condition not in while loop.

    $country = $_POST['Country'] ;
    $sql = "select * from country_details";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
    {
         $AllCountry = $row['CountryName']; 

         if($country==$AllCountry)
         {

            $Last_Insert_Country_Id=$row['CountryId'];
            break;

         }
         else
         {

            $date = date("Y/m/d");
            $sql = "insert into country_details (CountryName,CreatedDate) values ('$country','$date')";
            $query=mysql_query($sql);
            $Last_Insert_Country_Id=mysql_insert_id();
            break;
          }


  }
3
  • 1
    Try continue instead of break Commented Dec 17, 2013 at 5:57
  • What do you mean by exit from if condition.Do you want to go tho the next iteration.? Commented Dec 17, 2013 at 5:58
  • Intresting after the if/ you do not have any command so i think it's unnecessary to use continue. Commented Dec 17, 2013 at 6:30

2 Answers 2

1

When the if statements condition is matched, your break does nothing but break out of the loop. So by asking "break out of if statement" is redundant. If you want to skip the rest of the loops iteration, use continue instead, however it seems unnecessary:

When the if triggers, the else will not. So the iteration will be skipped either way.

Oh, and please sanitize your input. You are vulnerable to SQL injection! MYSQL_* is also deprecated, use MYSQLi_* or PDO.

Sign up to request clarification or add additional context in comments.

Comments

0

With continue you go to end of your current cycle, and start a new while iteration.

With break you exit your current while cycle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.