简体   繁体   中英

PHP - IF statement always True In While loop

Im Currently developing in the 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. I think maybe its only using the first value and not looping through $stock, if thats the case any ideas?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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