[英]PHP - IF statement always True In 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());
}
我不確定為什么函數會像它一樣在循環中聲明 - 更常見的是你會在任何這樣的循環之前聲明它。 我可能已經錯過了這一點,但我認為你可以沿着這些方向做點什么。
$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.