Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 31, 2023 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/723160fa409c04f76b53e38fa9e2f427 to your computer and use it in GitHub Desktop.
Save code-boxx/723160fa409c04f76b53e38fa9e2f427 to your computer and use it in GitHub Desktop.
PHP Export MYSQL Data To CSV

PHP EXPORT MYSQL DATA TO CSV

https://code-boxx.com/export-mysql-csv-php/

NOTES

  1. Create a test database and import 1-users.sql.
  2. Change the database settings in 2a-export-download.php and 2b-export-save.php to your own.
  3. Run 2a-export-download.php for a demo on export to a CSV download, 2b-export-save.php to save to a CSV file on the server.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `users` (
`user_id` bigint(20) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `user_email` (`user_email`),
ADD KEY `user_name` (`user_name`);
ALTER TABLE `users`
MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT;
INSERT INTO `users` (`user_id`, `user_email`, `user_name`) VALUES
(1, 'Jane Doe', 'jane@doe.com'),
(2, 'Joe Doe', 'joe@doe.com'),
(3, 'John Doe', 'john@doe.com'),
(4, 'Julie Doe', 'julie@doe.com'),
(5, 'Johan Doe', 'johan@doe.com'),
(6, 'Joanne Doe', 'joanne@doe.com'),
(7, 'Juliet Doe', 'juliet@doe.com'),
(8, 'June Doe', 'june@doe.com'),
(9, 'Juan Doe', 'juan@doe.com'),
(10, 'Jamir Doe', 'jamir@doe.com'),
(11, 'Jaden Doe', 'jaden@doe.com'),
(12, 'James Doe', 'james@doe.com'),
(13, 'Janus Doe', 'janus@doe.com'),
(14, 'Jason Doe', 'jason@doe.com'),
(15, 'Jay Doe', 'jay@doe.com'),
(16, 'Jeff Doe', 'jeff@doe.com'),
(17, 'Jenn Doe', 'jenn@doe.com'),
(18, 'Joah Doe', 'joah@doe.com'),
(19, 'Joyce Doe', 'joyce@doe.com'),
(20, 'Joy Doe', 'joy@doe.com'),
(21, 'Juke Doe', 'juke@doe.com'),
(22, 'Johnnie Doe', 'johnnie@doe.com'),
(23, 'Jim Doe', 'jim@doe.com'),
(24, 'Jess Doe', 'jess@doe.com'),
(25, 'Jabril Doe', 'jabril@doe.com');
<?php
// (A) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
$dbHost = "localhost";
$dbName = "test";
$dbChar = "utf8mb4";
$dbUser = "root";
$dbPass = "";
$pdo = new PDO(
"mysql:host=$dbHost;dbname=$dbName;charset=$dbChar",
$dbUser, $dbPass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]);
// (B) HTTP CSV HEADERS
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"export.csv\"");
// (C) GET USERS FROM DATABASE + DIRECT OUTPUT
$out = fopen("php://output", "w");
$stmt = $pdo->prepare("SELECT * FROM `users`");
$stmt->execute();
while ($row = $stmt->fetch()) { fputcsv($out, $row); }
fclose($out);
<?php
// (A) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
$dbHost = "localhost";
$dbName = "test";
$dbChar = "utf8mb4";
$dbUser = "root";
$dbPass = "";
$pdo = new PDO(
"mysql:host=$dbHost;dbname=$dbName;charset=$dbChar",
$dbUser, $dbPass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_NAMED
]);
// (B) CREATE EMPTY CSV FILE ON SERVER
$handle = fopen("export.csv", "w");
if ($handle === false) { exit("Error creating $csvFile"); }
// (C) GET USERS FROM DATABASE + WRITE TO FILE
$stmt = $pdo->prepare("SELECT * FROM `users`");
$stmt->execute();
while ($row = $stmt->fetch()) { fputcsv($handle, $row); }
fclose($handle);
echo "DONE!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment