简体   繁体   中英

How to exit from the if condition not in while loop in php?

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;
          }


  }

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.

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

With break you exit your current while cycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM