PHP - IF语句始终为True In While循环

时间:2016-10-12 09:45:06

标签: php arrays if-statement foreach while-loop

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

现在在我的while循环中,我有一个IF语句,由于某种原因,它当前总是触发为真。我想也许它只使用第一个值而不是循环通过$ 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());
      }

1 个答案:

答案 0 :(得分:0)

我不确定为什么函数会像它一样在循环中声明 - 更常见的是你会在任何这样的循环之前声明它。可能我可能错过了这一点,但我认为你可以沿着这些方向做点什么。

$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 ) */