having some issues outputting some SQL queries into CSV format. I can successfully do one query and write it to CSV, but I need to do a query within a query and I get that awesome infinite loop. Here's what I have so far
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$fp = fopen('php://output', 'w');
$result = mysqli_query($conn, "SELECT * FROM Customers")
or die("Error: ".mysqli_error($conn));
$row = mysqli_fetch_assoc($result);
if($row) {
fputcsv($fp, array_keys($row));
// reset pointer back to beginning
mysqli_data_seek($result, 0);
}
while($row = mysqli_fetch_assoc($result)) {
$orderResult = mysqli_query($conn,"SELECT * FROM Orders WHERE order_No = '".$row['customer_ID']."'")
or die("Error: ".mysqli_error($conn));
$rowOrder = mysqli_fetch_assoc($orderResult);
fputcsv($fp, array_keys($rowOrder));
mysqli_data_seek($result, 0);
$rowOrder = mysqli_fetch_assoc($orderResult);
fputcsv($fp, $row);
fputcsv($fp, $rowOrder);
}
fclose($fp);
The output i'm trying to achieve is:
Display customer header info in this row a.k.a first name, surname, email
next row displays info about customers e.g Tony, Mcguire, [email protected]
Display Tony's order header info e.g order number, order date
display Tony's order info e.g 5122, 6/3/2013
If I can get it all to display into 2 rows, that'd be even better.