Page 1 of 1

Double Data in CSV

Posted: Mon Jan 18, 2010 4:13 pm
by michaelk46
I have a script that exports data from a MySQL database into a CSV file. The only problem is that it is doubling the data from each field.

So if the data in cell A is supposed to be 'One Hundred Forty One' then it will put that in Cell 'A' and 'B'

Here is the Code:

Code: Select all

 
$a=0;
$check = $_POST['check'];
$number = count($check);
$fields = array('id', 'category', 'manufacturer', 'pagename', 'retailprice', 'salesprice', 'upc','sku', 'caption', 'image');
$fp = fopen('C:/wamp/www/Work/Upload/Files/csv/export/export' . ' ' . date('Y-m-d') . '.csv', 'w');
fputcsv($fp, $fields);
while ($a < $number)
    {
        $sql = 'SELECT pages.id, category.cat, manufacturer.manu, pages.pagename, pages.retailprice, 
                pages.salesprice, pages.upc, pages.sku, pages.caption, image.name FROM pages 
                INNER JOIN category ON pages.categoryid = category.id 
                INNER JOIN image ON pages.imageid = image.id
                INNER JOIN manufacturer ON pages.manufactureid = manufacturer.id WHERE pages.id = "'. $check[$a] .'"';
        $result = mysqli_query($link, $sql);
        $row = mysqli_fetch_array($result);
        fputcsv($fp, $row);
        ++$a;
        }
fclose($fp); 
 

Re: Double Data in CSV

Posted: Mon Jan 18, 2010 4:28 pm
by Eran
The default for mysql_fetch_array is to return both an associative indexed row and a numerically indexed row - basically two arrays with the same data but different indexes. Either set the second parameter in the function to the array type of your choosing, or use a different function, such as mysql_fetch_assoc() or mysql_fetch_row().
http://php.net/manual/en/function.mysql-fetch-array.php

Re: Double Data in CSV

Posted: Mon Jan 18, 2010 4:46 pm
by michaelk46
I see said the blind Man.. Thanks Dude