Results 1 to 3 of 3

Thread: [RESOLVED] 2 dimensional array to csv

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    right here
    Posts
    87

    Resolved [RESOLVED] 2 dimensional array to csv

    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:
    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
    If Not Now Then When

    If Not Here Then Where

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: 2 dimensional array to csv

    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:
    PHP Code:
        foreach ($list as $line)
        {
            
    fputcsv($file$line);
         } 

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    right here
    Posts
    87

    Re: 2 dimensional array to csv

    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
    If Not Now Then When

    If Not Here Then Where

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width