PHP - IF 语句在 While 循环中始终为真

Posted

技术标签:

【中文标题】PHP - IF 语句在 While 循环中始终为真【英文标题】:PHP - IF statement always True In While loop 【发布时间】:2017-02-21 01:43:42 【问题描述】:

我目前正在使用 adwords SDK 进行开发。 我从数据库中查询产品是否有库存,并将广告设置为暂停或启用。

现在在我的 while 循环中,我有一个 IF 语句,由于某种原因,它当前总是触发 true。我想也许它只使用第一个值而不是循环通过 $stock,如果是这样的话,有什么想法吗?

我需要它来遍历每个库存状态。如果启用了“有货”帖子,则将其设置为暂停

这是代码

$sql = "SELECT * FROM MYDB.MYTBL" ;
$result = mysqli_query($conn, $sql);

if ($result) 

while($row = mysqli_fetch_array($result))
  
    $adGroup = $row['adgroup'];
    $adGroupId = $row['adgroup_id'];
    $product = $row['product'];
    $stock = $row['stock'];
    $client = $row['client'];


    if ($stock === "In Stock") 

      if(!function_exists('UpdateAdGroupExample'))

        function UpdateAdGroupExample(AdWordsUser $user, $adGroupId) 
          // Get the service, which loads the required classes.
          $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION);

          // Create ad group using an existing ID.
          $adGroup = new AdGroup();
          $adGroup->id = $adGroupId;

          // Update the status.
          $adGroup->status = 'ENABLED';

          // Create operation.
          $operation = new AdGroupOperation();
          $operation->operand = $adGroup;
          $operation->operator = 'SET';

          $operations = array($operation);

          // Make the mutate request.
          $result = $adGroupService->mutate($operations);

          // Display result.
          $adGroup = $result->value[0];
          printf("Ad group with ID '%s' has updated default bid '$%s'.\n", $adGroup->id,
              $adGroup->status);

        

        try 
          // Get AdWordsUser from credentials in "../auth.ini"
          // relative to the AdWordsUser.php file's directory.
          $user = new AdWordsUser();
          $user->SetClientCustomerId('XXXXXXXXX');
          // Log every SOAP XML request and response.
          $user->LogAll();

          // Run the example.
          UpdateAdGroupExample($user, $adGroupId);
         catch (Exception $e) 
          printf("An error has occurred: %s\n", $e->getMessage());
        
      
      try 
        // Get AdWordsUser from credentials in "../auth.ini"
        // relative to the AdWordsUser.php file's directory.
        $user = new AdWordsUser();
        $user->SetClientCustomerId('XXXXXXXXX');
        // Log every SOAP XML request and response.
        $user->LogAll();

        // Run the example.
        UpdateAdGroupExample($user, $adGroupId);
       catch (Exception $e) 
        printf("An error has occurred: %s\n", $e->getMessage());
      

【问题讨论】:

你有 2 个 ifs :D 如果它是 if ($stock === "In Stock") 则从中删除 1 ` = ` @KikiTheOne 哈哈 ;) 在您的内部函数中,您覆盖变量 $result - 也许这是一个问题。如果没有,则意味着您的所有记录都是“有货”:) 让我问 1 个问题。为什么你在 WhileLoop 中声明函数?而不是在循环之外,只需调用一次? @nospor 我认为这将是错误,因为他在 $result fetch_array 时循环。因此,如果结果不再是 samy,则没有任何内容可以循环。要检查它是否在第一个循环后卡住,请在 while 循环中放置一个 echo $row['stock'] 。如果只有一个值,你就知道循环坏了。 【参考方案1】:

我不确定为什么函数会像这样在循环中声明 - 更常见的是,您会在任何此类循环之前声明它。我可能没有抓住重点,但我认为你可以按照这些思路做点什么。

$sql = "SELECT * FROM MYDB.MYTBL" ;
$result = mysqli_query( $conn, $sql );

if( $result ) 

    if( !function_exists( 'UpdateAdGroupExample' ) )

        function UpdateAdGroupExample( AdWordsUser $user, $adGroupId ) 
          // Get the service, which loads the required classes.
          $adGroupService = $user->GetService( 'AdGroupService', ADWORDS_VERSION );

          // Create ad group using an existing ID.
          $adGroup = new AdGroup();
          $adGroup->id = $adGroupId;

          // Update the status.
          $adGroup->status = 'ENABLED';

          // Create operation.
          $operation = new AdGroupOperation();
          $operation->operand = $adGroup;
          $operation->operator = 'SET';

          $operations = array( $operation );

          // Make the mutate request.
          $result = $adGroupService->mutate( $operations );

          // Display result.
          $adGroup = $result->value[0];

          printf( "Ad-Group with ID '%s' has updated default bid '$%s'.\n", $adGroup->id, $adGroup->status );
        
    



    while( $row = mysqli_fetch_array( $result ) ) 
        /* appear unused ~ is there further code in the loop not shown? */
        #$adGroup = $row['adgroup'];
        #$product = $row['product'];
        #$client = $row['client'];

        $adGroupId = $row['adgroup_id'];
        $stock = trim( $row['stock'] );


        if ( $stock == "In Stock" ) 

            try 
                $user = new AdWordsUser();
                if( $user )

                    $user->SetClientCustomerId('XXXXXXXXX');
                    $user->LogAll();

                    UpdateAdGroupExample( $user, $adGroupId );
                 else 
                    throw new Exception('User could not be created');
                
             catch( Exception $e ) 
                printf( "An exception has occurred: %s\n", $e->getMessage() );
            
         else 
            /* debug what the value of $stock is */
            printf("stock: %s\n",$stock);

        /* end stock check */

    /* end while */

/* end if( $result ) */

【讨论】:

仍然不确定这条线是否 $result = $adGroupService->mutate( $operations );不会造成任何问题,因为您正在覆盖结果...愿你比我知道的更多 x: @RamRaider - 我认为你的答案肯定会起作用我只需要能够使用这一行 $adGroup->status = 'ENABLED';在 if ( $stock == "In Stock" ) 语句中。

以上是关于PHP - IF 语句在 While 循环中始终为真的主要内容,如果未能解决你的问题,请参考以下文章

PHP循环控制语句之“while”

while循环语句

如何在while循环中处理if语句 - Python

while语句(内有实操)

带有嵌套 if/else 语句的 While 循环

在while循环中使用if语句使R跳过某些索引值