循环执行sql查询的结果

时间:2013-06-14 21:31:51

标签: php mysql arrays

我有一个返回多行的查询。我似乎无法找到一种方法来存储$ params数组中的行。有没有办法循环抛出并存储$ params变量

中的每一行
$aResult = $db->exec_sql($sql);  
$params = array(
  // where $aResult[o]'' would be row 1 [1] row 2 etc. //
  'store_id' => $aResult[]['iStoreID'],
  'user_id' => $aResult[]['iUserID'],
  'store_name' => $aResult[]['cStoreName'],
  'store_url' => $aResult[]['cStoreURL'],
  'rid' => $aResult[]['cRID'],
  'affiliate_id' => $aResult[]['iAffiliateID'],
  'team_id' => $aResult[]['iTeamID'],
  'bizOP' => $aResult[]['cDefaultBizOpp'],
  'showBizOPp' => $aResult[]['iShowBizOppDropdown'],
  'boPosting' => $aResult[]['iEnableBOPosting'],
  'brandinglevel' => $aResult[]['iBrandingLevel']
);

谢谢你的帮助

3 个答案:

答案 0 :(得分:2)

就这么简单:

$params = array();
foreach($aResult as $row) {
    $params[] = array(
        'store_id' => $row['iStoreID'],
        'user_id' => $row['iUserID'],
        'store_name' => $row['cStoreName'],
        'store_url' => $row['cStoreURL'],
        'rid' => $row['cRID'],
        'affiliate_id' => $row['iAffiliateID'],
        'team_id' => $row['iTeamID'],
        'bizOP' => $row['cDefaultBizOpp'],
        'showBizOPp' => $row['iShowBizOppDropdown'],
        'boPosting' => $row['iEnableBOPosting'],
        'brandinglevel' => $row['iBrandingLevel']
    );
}

答案 1 :(得分:0)

在不知道结果数组的确切结构的情况下,我猜你需要这样的东西:

<?php

$params  = array();
$mapping = array(
    'store_id' => 'iStoredID',
    'user_id'  => 'iUserID',
// and so on...
);

foreach ($aResult as $row) {
   $tempRow = array();
   foreach ($row as $key => $value) {
       $paramKey           = isset($mapping[$key]) ? $mapping[$key] : $key;
       $tempRow[$paramKey] = $value;
   }
   $params[] = $tempRow;
}

答案 2 :(得分:-1)

我像这样使用它

$aResult = mysql_query($sql);

while($data = mysql_fetch_array($result)) {
     'store_id' = $aResult['iStoreID'];
}

至少这是想法

相关问题