0

I have a query that returns multiple rows. I can't seem to find a way to store the rows in the $params array. Is there a way to loop throw and store each row in the $params variable

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

thank you for your help

4
  • Could you tell us what the exec_sql return? Commented Jun 14, 2013 at 21:34
  • var_dump($aResult); = ? Commented Jun 14, 2013 at 21:34
  • 40=[Array containing 4 elements] 0=[Array containing 14 elements] iInvoice="" yAmt="" cNameF="" cNameL="" cEmail="" ... That is the dump of what the sql is doing . . . obvoiusly there are more columns and data has been removed but i think it has what is necessary. Thank you Commented Jun 14, 2013 at 21:51
  • To make it easier for people to read you can use the Edit and update it with the dump. Commented Jun 14, 2013 at 23:13

3 Answers 3

2

As simple as that:

$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']
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Without knowing the exact structure of the result array i guess you need something like this:

<?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;
}

Comments

-1

I use it like this

$aResult = mysql_query($sql);

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

At least that is the idea

1 Comment

Stop using mysql_* functions. This extension is deprecated, and will be soon removed from php. Use mysqli instead or pdo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.