PDA

Click to See Complete Forum and Search --> : [RESOLVED] 2 dimensional array to csv


brianbaart
May 8th, 2009, 07:23 AM
Dear All,

I need to get a query from MySQL to a csv file.
But I need to add some data and empty variables into the csv line.
I get a file with 9 lines (which is correct) but the lines contain just the word "array"
I have following code:

<?php

include("conf.php");

$con = mysql_connect($host, $user, $password);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($name, $con);

$sql = "SELECT * FROM D2D WHERE GeExporteerd = 'NEE' ORDER BY D2D_ID";

$result = mysql_query($sql);

$list = array();

while($row = mysql_fetch_array($result))
{
//it goes wrong here, I think. I need to add some variables into the array //here.
$list[] = array($row['D2D_ID'],$row['CallNummer']);
}

$file = fopen($CSV_Adres . "Contact.csv","w");

foreach ($list as $line)
{
fputcsv($file,split(',',$line));
}

fclose($file);

mysql_close($con);

?>


What did I do wrong?
I have been testing all day.
Thanks in advance,
Brian

kows
May 8th, 2009, 10:52 PM
well, you're defining $list as an array of values of $row[2d2_id] and $row[callnumber]. then, when you are writing the CSV file, $line is an array and you are trying to split an array into an array using a comma delimiter, which.. just wouldn't work.

instead, do:
foreach ($list as $line)
{
fputcsv($file, $line);
}

brianbaart
May 9th, 2009, 06:11 AM
Dear All,

Thanks Kow, I have been testing and trying all day but didn't look at that line because I had used it before and it is/was working.

Looking back now I feel stupid.

Thanks again,
Brian