简体   繁体   English

如何从if条件不在PHP的while循环中退出?

[英]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. 如何从if条件不在while循环中退出?。当国家/地区与当时的存储国家/地区匹配时从if条件不在while循环中退出。

    $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. 如果满足if语句条件,则break只会中断循环。 So by asking "break out of if statement" is redundant. 因此,通过询问“打破if语句”是多余的。 If you want to skip the rest of the loops iteration, use continue instead, however it seems unnecessary: 如果要跳过循环的其余部分,请改用continue ,但这似乎是不必要的:

When the if triggers, the else will not. 如果if触发,则else不会触发。 So the iteration will be skipped either way. 因此,迭代将以任何一种方式被跳过。

Oh, and please sanitize your input. 哦,请清理您的输入。 You are vulnerable to SQL injection! 您容易受到SQL注入的攻击! MYSQL_* is also deprecated, use MYSQLi_* or PDO. 不建议使用MYSQL_ *,请使用MYSQLi_ *或PDO。

With continue you go to end of your current cycle, and start a new while iteration. 继续,您将转到当前循环的结尾,并开始一个新的while迭代。

With break you exit your current while cycle. 休息时退出当前循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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