Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

PHP CSV File Export Using fputcsv()

<?php
$conn = mysqli_connect("localhost", "root", "test", "phppot_examples");

$filename = "toy_csv.csv";
$fp = fopen('php://output', 'w');

$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='phppot_examples' AND TABLE_NAME='toy'";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_row($result)) {
	$header[] = $row[0];
}	

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);

$query = "SELECT * FROM toy";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_row($result)) {
	fputcsv($fp, $row);
}
exit;
?>
Source by phppot.com #
 
PREVIOUS NEXT
Tagged: #PHP #CSV #File #Export #Using
ADD COMMENT
Topic
Name
3+7 =