-1

I am trying to save the rows (results) from an SQL query to a csv file. I am using array push in order to put the results in a list. Later I put the data from this list to my csv file.

My code :

    while ($row = $query->fetch_assoc()) 
{
    echo sprintf( $row['campaign']);
    array_push($list, $row['campaign']);

}

The results are there because sprintf works. The problem is with the syntax of array_push. I even tried :

array_push($list, array(''.$row['campaign']);

I am getting an error:

fputcsv() expects parameter 2 to be array

The full code is here :

    $list = array 
    (
        array('old_campaign_name', 'new_campaign_name') 
    );

// table 1
$sql = ('select distinct(campaign) as campaign from '.$table1.'');

// Run the query
$query = $Db->query($sql);

// Check for SQL errors
if ($Db->error) 
{
    return ($Db->error);
}

// Put data in the list

while ($row = $query->fetch_assoc()) 
{
    echo sprintf( $row['campaign']);
    array_push($list,$row['campaign'],'');

}

$fp = fopen($location, 'w');

foreach ($list as $fields) 
{
    fputcsv($fp, $fields);
}

fclose($fp);
Datacrawler
  • 2,472
  • 5
  • 38
  • 81
  • Are you intending to save the entire row, or just the `campaign` value from the row? Either way, it looks like you probably don't need `array_push`. Just `$list[] = $row;` or `$list[] = $row['campaign'];` should work. – Don't Panic Dec 22 '15 at 16:01
  • I need to use array_push. Because in my csv file I do not want to lose the previous attributes. I just want to add the new ones. Probably the problem is with the fputcsv. – Datacrawler Dec 22 '15 at 16:03
  • Why does your heading row have two columns (old and new campaign names), but you're only putting one column in the data rows? – Barmar Dec 22 '15 at 16:14
  • @Barmar Because the second column has to be blank to add things manually. – Datacrawler Dec 22 '15 at 16:14

3 Answers3

1

When you initially create the $list array, it is an array containing one array. But when you add more values to it from your query results, you are pushing strings onto the end of it, not arrays. In effect, you will be making something like

$list = array (
    array('old_campaign_name', 'new_campaign_name'),
    'first campaign',
    'second campaign',
    'etc.',
    ...
);

Because of this, when you loop over $list, the first value should work with fputcsv, because it is an array, but any subsequent values will be strings instead of arrays and will cause the error you are seeing.

You should be able to fill the $list like this:

while ($row = $query->fetch_assoc()) {
    $list[] = $row;
}

$list[] = $row will not overwrite the values previously in $list. From the PHP documentation for array_push:

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

Don't Panic
  • 37,589
  • 9
  • 55
  • 71
1

As the error says, fputcsv expects each row that you put to be an array, so it can write it out with commas separating the elements. $list should be a 2-dimensional array, so you need to push an array onto it when you're building it.

while ($row = $query->fetch_assoc() {
    $list[] = array($row['campaign']);
}

BTW, $list[] = x is equivalent to array_push($list, x).

Barmar
  • 596,455
  • 48
  • 393
  • 495
0

It works like this :

while ($row = $query->fetch_assoc()) 
{
    // array_push($list,$row['campaign'],'');
    array_push($list,array($row['campaign'], ''));

}
Datacrawler
  • 2,472
  • 5
  • 38
  • 81