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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.