export data as csv in php code example
Example 1: php modify csv file
$myfile = 'sample_csv.csv';
$fin = fopen($myfile, 'r');
$data = array();
$data[] = fgetcsv($fin, 1000);
while ($line = fgetcsv($fin, 1000)) {
echo join(', ', $line).'<br>';
for($i = 4, $k = count($line); $i < $k; $i++) {
if ($line[$i] < 1000) {
$line[$i] = 10000;
}
}
$data[] = $line;
}
fclose($fin);
$fout = fopen($myfile, 'w');
foreach ($data as $line) {
fputcsv($fout, $line);
}
fclose($fout);
Example 2: how to export php mysql data to csv through php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "test";
$con = new mysqli($host, $user, $password, $database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>