Mysql Query to CSV Export
<?php
include "../config.php";
$query = '
SELECT order_tickets.order_id,ticket.event_id, order_tickets.ticket_tname as full_name, orders.address,
orders.city, orders.state, orders.zipcode, orders.email,order_tickets.age, order_tickets.tshirt_size, order_tickets.boxname,
order_tickets.ticket_number, order_tickets.ticket_team_name, order_tickets.a1, order_tickets.a2, order_tickets.a3, order_tickets.a4, order_tickets.a5, order_tickets.a6, order_tickets.a7, ticket.ticket_name, ticket.quantity, ticket.sold,
orders.amount, orders.fees, orders.date_added
FROM order_tickets
INNER JOIN ticket
ON order_tickets.ticket_id = ticket.ticket_id
INNER JOIN orders
ON order_tickets.order_id = orders.order_id
WHERE orders.event_id = ' . $_GET["id"] ;
$result = mysql_query($query);
if (!$result) die('Couldn\'t fetch records');
// how many fields on the table:
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
// returns the name of the specified field index
$headers[] = mysql_field_name($result , $i);
}
// $fp: file pointer
$fp = fopen('php://output', 'w'); // write permissions
if ($fp && $result)
{
header('Content-Type: text/csv');
// change the export filename here
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
//format line as CSV and write to file pointer $fp
fputcsv($fp, array_values($row));
}
die;
}