Real-Time Domain Availability Checker in PHP (AJAX + WHOIS + DNS)
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
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.
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
Learn how to build an advanced AJAX contact form with PHP that sends email without page reload. Includes modern UI design, form validation, and secure email handling using PHP's mail() function.
Learn how to remove .php, .html, and .htm extensions from URLs using .htaccess. Create clean, SEO-friendly URLs on Apache servers with our step-by-step guide.