2016-09-29 44 views
2

成功解決:工作代碼記錄在本文的底部。將變量賦值給SQL命令結果

我目前正試圖運行一個SQL命令來抓取數據庫表中的所有數據,並通過使用變量名稱的API調用發送它。

我遇到的問題是分配「$行」下的字段的值來分隔變量,所以我可以將它們放在我的foreach循環中,並將它們全部發送到API調用。任何人都可以啓發我,我做錯了什麼

我敢肯定我要錯的行是我的commandbuilder,然後將變量分配給行內的數據。

我覺得這個問題行可以

while ($row = mysql_fetch_assoc()) { 
    $emails = $row['email_address']; 
    $names = $row['forename']; 
} 

完整的代碼如下。

public function actionImportSubscribers($cm_list_id){ 
    //need to pass through cm_list_id instead 
    $cm_list_id = Yii::app()->getRequest()->getQuery('cm_list_id'); 
    $model =$this->loadModelList($cm_list_id); 
    $listID = $model->cm_list->cm_list_id; 
    $row = Yii::app()->db->createCommand() 
    ->select('email_address, forename') 
    ->from('tbl_cm_subscribers') 
    ->where('cm_list_id=:id', array(':id' => $cm_list_id)) 
    ->queryAll(); 
    while ($row = mysql_fetch_assoc()) { 
     $emails = $row['email_address']; 
     $names = $row['forename']; 
    } 
    $customFieldArray = array(); 
    $addFieldsToList = array(); 
    foreach (array_combine($emails, $names) as $name => $email) { 
     $addFieldsToList[] = array('EmailAddress' => $email,'Name' => $name,'CustomFields' => $customFieldArray); 
    } 
    $auth = array('api_key' => ''); 
    $wrap = new CS_REST_Subscribers($listID, $auth); 
$result = $wrap->import(array($addFieldsToList), false); 

工作代碼如下

public function actionImportSubscribers($cm_list_id){ 

     //need to pass through cm_list_id instead 
     $cm_list_id = Yii::app()->getRequest()->getQuery('cm_list_id'); 

     $model =$this->loadModelList($cm_list_id); 

     $listID = $model->cm_list->cm_list_id; 

     $result = Yii::app()->db->createCommand() 
          ->select('email_address, forename') 
          ->from('tbl_cm_subscribers') 
          ->where('cm_list_id=:id', array(':id' => $cm_list_id)) 
          ->queryAll(); 
     $emails=array(); 
     $names=array(); 
     foreach ($result as $row) { 
      $emails[] = $row['email_address']; 
      $names[] = $row['forename']; 
     } 

     require_once 'protected/extensions/createsend-php-5.0.1/csrest_subscribers.php'; 

     $auth = array('api_key' => ''); 

     foreach (array_combine($emails, $names) as $email => $name) { 

      $wrap = new CS_REST_Subscribers($listID, $auth); 

       $result = $wrap->import(array(
        array(
         'EmailAddress' => $email, 
         'Name' => $name, 
        ), 
       ), false); 
     } 

      echo "Result of POST /api/v3.1/subscribers/{list id}/import.{format}\n<br />"; 
      if($result->was_successful()) { 
       echo "Subscribed with results <pre>"; 
       var_dump($result->response); 
      } else { 
       echo 'Failed with code '.$result->http_status_code."\n<br /><pre>"; 
       var_dump($result->response); 
       echo '</pre>'; 

       if($result->response->ResultData->TotalExistingSubscribers > 0) { 
        echo 'Updated '.$result->response->ResultData->TotalExistingSubscribers.' existing subscribers in the list';   
       } else if($result->response->ResultData->TotalNewSubscribers > 0) { 
        echo 'Added '.$result->response->ResultData->TotalNewSubscribers.' to the list'; 
       } else if(count($result->response->ResultData->DuplicateEmailsInSubmission) > 0) { 
        echo $result->response->ResultData->DuplicateEmailsInSubmission.' were duplicated in the provided array.'; 
       } 

       echo 'The following emails failed to import correctly.<pre>'; 
       var_dump($result->response->ResultData->FailureDetails); 
      } 
      echo '</pre>'; 
     // } 

    } 
+0

嘗試改變'陣列( ':身份證'=> $ cm_list_id)''到陣列('身份證'=> $ cm_list_id)' – amow

+0

這部分工作正常我認爲,我相信問題是($ row = mysql_fetch_assoc()){ $ emails = $ row ['email_address']; $ names = $ row ['forename']; } 但我找不到其他任何方式來通過 – DevAL

+0

'$ emails [] = $ row ['email_address']; $ names [] = $ row ['forename'];'? – amow

回答

1

我不知道這是否解決您的問題,但你有一些錯誤存在。
mysql_fetch_assoc()需要param,從mysql_query函數返回的資源。

在創建$ emails和$ names變量的部分地方,如果您嘗試創建數組,但是您將以這種方式始終獲得單值,那麼您是如何做到這一點的。這是舉例來說,如果你會使用mysql_fetch_assoc,你不能用mysql_fetch_assoc結合queryAll()

$emails=array(); 
$names=array(); 
$result=mysql_query("SELECT email_address, forename FROM tbl_cm_subscribers where cm_list_id='$cm_list_id'"); 
while ($row = mysql_fetch_assoc($result)) { 
    $emails[] = $row['email_address']; 
    $names[] = $row['forename']; 
} 

queryAll()方法返回數組,我不知道,但Yii中我想這是你需要做什麼

$result = Yii::app()->db->createCommand() 
        ->select('email_address, forename') 
        ->from('tbl_cm_subscribers') 
        ->where('cm_list_id=:id', array(':id' => $cm_list_id)) 
        ->queryAll(); 
$emails=array(); 
$names=array(); 
foreach ($result as $row) { 
    $emails[] = $row['email_address']; 
    $names[] = $row['forename']; 
} 

或者,如果您不需要結果的數組,然後使用$電子郵件和$名稱不[]

+0

真棒歡呼聲,我不得不改變它UPA一點,添加在我的API循環發送 的foreach(array_combine($電子郵件,$名)爲$電子郵件=> $名){ } 更新OP具有完整的答案。 – DevAL