How to Build a Browser OCR Tool with Tesseract.js
Learn how to build a browser-based OCR app using Tesseract.js. Extract text from images directly in the browser with a simple client-side setup.
Suggested:
Learn how to export MySQL data to CSV and Excel using PHP. This guide covers advanced techniques, performance tips, and security best practices for seamless data export in web applications.
Exporting data from a MySQL database to Excel or CSV files is a common requirement in web applications. Whether you're building an admin panel, data reporting dashboard, or automating a backup process, efficient data export is a must-have feature.
In this blog, we’ll walk through advanced techniques for exporting MySQL data to CSV and Excel formats using PHP, with best practices for performance, security, and file handling.
Table of contents [Show]
PhpSpreadsheet for Excel (.xlsx) export<?php
$dsn = 'mysql:host=localhost;dbname=your_database;charset=utf8mb4';
$username = 'your_user';
$password = 'your_password';
try {
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
$query = "SELECT id, name, email, created_at FROM users";
$stmt = $pdo->prepare($query);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);<?php
$filename = "users_export_" . date("Y-m-d_H-i-s") . ".csv";
header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=\"$filename\"");
$output = fopen('php://output', 'w');
// Output header row
fputcsv($output, array_keys($data[0]));
// Output data rows
foreach ($data as $row) {
fputcsv($output, $row);
}
fclose($output);
exit;
?>
composer require phpoffice/phpspreadsheet<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Header
$sheet->fromArray(array_keys($data[0]), null, 'A1');
// Data rows
$sheet->fromArray($data, null, 'A2');
// File output
$filename = "users_export_" . date("Y-m-d_H-i-s") . ".xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=\"$filename\"");
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
?>
if ($_GET['format'] === 'csv') {
exportToCSV($data);
} elseif ($_GET['format'] === 'excel') {
exportToExcel($data);
} else {
echo "Invalid format selected.";
}yield or iterators for memory efficiencyset_time_limit(0) for long-running processes$stmt = $pdo->prepare('SELECT id, name FROM huge_table');
$stmt->execute();
header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=\"huge_export.csv\"");
$output = fopen('php://output', 'w');
$isFirst = true;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($isFirst) {
fputcsv($output, array_keys($row));
$isFirst = false;
}
fputcsv($output, $row);
}
fclose($output);
exit;
Exporting MySQL data to Excel or CSV using PHP can be quick and robust with the right tools. For simple data dumps, CSV is efficient and sufficient. For well-formatted reports, use PhpSpreadsheet to create professional Excel files.
Whether you’re building a dashboard or automating business reports, exporting data reliably is a key step in making your app more powerful and user-friendly.
Learn how to build a browser-based OCR app using Tesseract.js. Extract text from images directly in the browser with a simple client-side setup.
Step-by-step guide to creating a PHP backend that removes image backgrounds using Remove.bg API. Ideal for developers building automated image tools
Learn how to create a real-time PHP chat app using MySQL and JavaScript with user login, typing status, chat history deletion, and a modern, responsive UI.