gpt4 book ai didi

php - 将变量分配给 SQL 命令结果

转载 作者:行者123 更新时间:2023-11-29 02:47:45 24 4
gpt4 key购买 nike

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

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

我遇到的问题是将“$row”下字段的值分配给单独的变量,这样我就可以将它们放在我的 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>';
// }

}

最佳答案

我不知道这是否能解决您的问题,但您那里几乎没有错误。
mysql_fetch_assoc() 需要参数,从 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'];
}

或者,如果您不需要结果数组,则使用不带 [] 的 $emails 和 $names

关于php - 将变量分配给 SQL 命令结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39766638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com