将变量分配给SQL命令结果

时间:2016-09-29 09:33:15

标签: php mysql

成功解决:本帖子底部注明了工作代码。

我目前正在尝试运行SQL命令从数据库表中获取所有数据,并使用变量名称通过API调用发送它。

我遇到的问题是在" $ row"下分配字段的值。分离变量,然后我可以将它们放在我的foreach循环中,并将它们全部发送到API调用。任何人都可以告诉我我做错了什么

我确定我出错的行是我的命令构建者,然后将变量分配给行内的数据。

我觉得问题可能是

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>';
        // }

    }

1 个答案:

答案 0 :(得分:1)

我不知道这是否解决了你的问题,但你的错误很少 mysql_fetch_assoc()需要param,mysql_query函数返回的资源。

在创建$ emails和$ names变量的部分中,如果您尝试创建数组,那么您将以这种方式获得单一值,如何完成它。这是一个例子,如果你将使用mysql_fetch_assoc,你不能将queryAll()与mysql_fetch_assoc结合起来

$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'];
}

或者,如果您不需要一系列结果,请使用$ email和$ name,而不要使用[]