PHP Export to CSV

Posted by Ali Hamra on Stack Overflow See other posts from Stack Overflow or by Ali Hamra
Published on 2012-09-07T21:30:48Z Indexed on 2012/09/07 21:38 UTC
Read the original article Hit count: 222

Filed under:
|
|
|
|

I'm not really familiar with PHP exporting to excel or csv, but I'm using PHP MySQL for a local point of sale. According to the code below, this actually works..But not in the way it should be ! All records are placed as 1 row inside the csv file, how can i fix that ? Also, How would I stop overwriting the same file...I mean When I click on a Button to export the csv, it should check if there is an existing csv file, If there is--Create new one !

Thank You

require_once('connect_db.php');

$items_array = array();

$result = mysql_query("SELECT * FROM sold_items");

while($row = mysql_fetch_array($result))
{
    $items_array[] = $row['item_no'];

    $items_array[] = $row['qty'];
}

$f = fopen('C:/mycsv.csv', 'w');
fputcsv($f, $items_array);
fclose($f);

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql