繁体   English   中英

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

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

如何从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;
          }


  }

如果满足if语句条件,则break只会中断循环。 因此,通过询问“打破if语句”是多余的。 如果要跳过循环的其余部分,请改用continue ,但这似乎是不必要的:

如果if触发,则else不会触发。 因此,迭代将以任何一种方式被跳过。

哦,请清理您的输入。 您容易受到SQL注入的攻击! 不建议使用MYSQL_ *,请使用MYSQLi_ *或PDO。

继续,您将转到当前循环的结尾,并开始一个新的while迭代。

休息时退出当前循环。

暂无
暂无

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

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