Double Data in CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Double Data in CSV

Post 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); 
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Double Data in CSV

Post 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
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: Double Data in CSV

Post by michaelk46 »

I see said the blind Man.. Thanks Dude
Post Reply