The following code output the sql query result into a csv file :
CODE
<?php
// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
//connection file
include("connection.php");
$query = 'SELECT resting_blood_sugar,serum_cholesterol,thalach,oldpeak,result,date from hd_test WHERE patient_id = 2';
$output = fopen('N:\oldpatientgraph.csv', 'w');
$result = mysqli_query($link,$query);
// loop over the rows, outputting them
while ($row = mysqli_fetch_assoc($result)) fputcsv($output, $row);
?>
OUTPUT
The csv file contains output in following format :
1,1,1,1,1,11-1-2015 #line1
2,3,1,4,2,1-1-2000 #line2
Whereas i want to output the result in one line as follows :
1,1,1,1,1,11-1-2015,2,3,1,4,2,1-1-2000 #line1
Anyone here who knows how to do this ??
just concancenate the row, like below :
// loop over the rows, outputting them
$arr1 = array();
while ($row = mysqli_fetch_assoc($result)){
$arr1 = array_merge($arr1,array_values($row));
}
fputcsv($output,$arr1);
You can try this
$output = fopen('N:\oldpatientgraph.csv', 'w');
$csvString = '';
$index = 0;
while ($row = mysqli_fetch_assoc($result)){
if ($key) {
$csvString.=',';
}
$csvString .= implode(',',array_values($row)); #Array values to string
$index = 1;
}
fwrite($output, $csvString); #Write string to file
fclose($output);
Note : Above code will give you all data in 1 line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.