简体   繁体   English

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

[英]PHP - IF statement always True In While loop

Im Currently developing in the adwords SDK. 我目前正在开发adwords SDK。 Im querying whether the products are in stock from the database and setting the ad to either paused or enabled. 我正在查询数据库中的产品是否有库存,并将广告设置为暂停或启用。

Now in my while loop i have an IF statement that is currently always firing true for some reason. 现在在我的while循环中,我有一个IF语句,由于某种原因目前总是触发真实。 I think maybe its only using the first value and not looping through $stock, if thats the case any ideas? 我想也许它只使用第一个值而不是循环通过$ stock,如果是这样的话任何想法?

I need it to run through each stock status. 我需要它来贯穿每个库存状态。 IF 'In Stock' post enabled else set it to paused 如果“有库存”帖子已启用,则将其设置为暂停

Heres the code 继承人的代码

$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());
      }

I'm not sure why the function would be declared within a loop like it was - more commonly you would declare it before any such loop. 我不确定为什么函数会像它一样在循环中声明 - 更常见的是你会在任何这样的循环之前声明它。 It's likely I may have missed the point but I'd have thought you could do something along these lines. 我可能已经错过了这一点,但我认为你可以沿着这些方向做点什么。

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

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

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