Difficulty using a mysql query in a for loop using PHP

Sam Comber

I'm having difficulty using a MySQL query in a for loop to produce an array of IDs. Essentially, I have an array of parent IDs which I feed into the query via a for loop and am expecting to return an array of campaign IDs.

Inside the for loop I can echo every campaign ID for every parent ID but can't seem to access this array outside of the loop. I'm assuming this is a scoping issue, and I've fiddled with the code but aren't able to access an array of campaign_ids in its entirety outside of the loop.

My code is as follows:

$campaigns[] = array();
for ($i=0; $i < count($multIO); $i++) {

  $sql = 'SELECT DISTINCT campaign_id
    FROM prod_appnexus.fact_network_analytics_feed_aggregated_364
    WHERE insertion_order_id = '.$multIO[$i]['IO_ID'].';';

  $retval = mysql_query($sql, $conn);
  while ($row = mysql_fetch_array($retval)) {
    $campaigns = $row['campaign_id'];
    echo print_r($campaigns);
  };
};

Does anybody know how I can troubleshoot this?

Any comments would be greatly appreciated!

Thanks,

Sam

Santa's helper

Change the line with this:

$campaigns[] = $row['campaign_id'];

or:

$campaigns[$multIO[$i]['IO_ID']][] = $row['campaign_id'];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related