1,"John Deo",Four,75,female
2,"Max Ruin",Three,85,male
We will use our student table for this example and show how to download or save the CSV file.
<?php
require "config.php"; // Connection string
dataConnect(); // Connect to Database
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="my-student-data.csv"");
$data = "";
$query = $dbo->prepare("select * FROM student");
$query->execute();
for($i=0; $row = $query->fetch(PDO::FETCH_NUM); $i++){
$data.="$row[0],$row[1],$row[2],$row[3],$row[4]"."\n";
}
unset($dbo);
unset($query);
echo $data;
?>
In the above script PDO data connection string and PDO class to collect records
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.