Welcome to a quick tutorial on how to import and export CSV with PHP and NodeJS. Need to generate a CSV report? Want to export a list from the database? Or need to import CSV data into the database? Well, it really isn’t too difficult. Let us walk through some simple examples – Read on!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
- Download and unzip into your project folder.
- Create a dummy database and import
1a-users.sql
. - Open the terminal, and install the required modules –
npm install mysql csv
. - Run
2-import.js
for the import demo,3-export.js
for the export demo – Remember to change the database settings to your own.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
MYSQL IMPORT & EXPORT CSV
All right, let us now get into the examples of how to import a CSV into MySQL, and export MySQL data to CSV.
PART 1) DUMMY DATA
1A) DUMMY USER DATABASE
CREATE TABLE `users` (
`user_id` bigint(20) NOT NULL,
`user_name` varchar(255) NOT NULL,
`user_email` 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, AUTO_INCREMENT=1;
This is the dummy user table that we will use as an example. Very simple, only has 3 fields:
user_id
The primary key, auto-increment.user_name
User name.user_email
User email, unique field.
1B) DUMMY CSV
Job Doe,job@doe.com
Joe Doe,joe@doe.com
Joi Doe,joi@doe.com
Jon Doe,jon@doe.com
Joy Doe,joy@doe.com
Don’t think this needs an explanation… We will be importing these 5 entries into the above table.
PART 2) IMPORT CSV TO MYSQL
// (A) LOAD MODULES
// npm install mysql csv
// https://www.npmjs.com/package/mysql
// https://www.npmjs.com/package/csv-parse
const mysql = require("mysql"),
fs = require("fs"),
csv = require("csv-parse");
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
// (C) IMPORT CSV
// (C1) READ & IMPORT ROW-BY-ROW
fs.createReadStream("1b-users.csv")
.pipe(csv.parse())
.on("data", row => db.query(
"INSERT INTO `users` (`user_name`, `user_email`) VALUES (?, ?)",
row, (err, results, fields) => {
console.log(row);
if (err) { console.error(err.message); }
else { console.log("USER ID:" + results.insertId); }
}
))
// (C2) CLOSE DATABASE CONNECTION
.on("end", () => {
db.end();
console.log("Import complete");
});
This may seem intimidating to some beginners, but keep calm and look carefully. It is as straightforward as can be:
- We load the required modules –
mysql
,fs
, andcsv-parse
. - Connect to the database, and remember to change the settings to your own.
- Read the CSV file line-by-line, and insert them into the database.
Yep, that’s all.
PART 3) EXPORT MYSQL TO CSV
// (A) LOAD MODULES
// npm install mysql csv
// https://www.npmjs.com/package/mysql
// https://www.npmjs.com/package/csv-stringify
const mysql = require("mysql"),
fs = require("fs"),
csv = require("csv-stringify");
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
// (C) CREATE CSV FILE
const writer = fs.createWriteStream("export.csv");
// (D) EXPORT MYSQL
// (D1) WRITE ROW-BY-ROW
db.query("SELECT * FROM `users`")
.on("error", err => console.error(err))
.on("result", row => {
db.pause();
csv.stringify([[
row["user_id"], row["user_name"], row["user_email"]
]], (err, output) => {
console.log(row);
writer.write(output);
db.resume();
});
})
// (D2) EXPORT COMPLETE
.on("end", () => {
db.end(); writer.end();
console.log("Export complete");
});
Not going to explain line-by-line again, this one is straightforward.
- Load the required modules,
mysql
,fs
, andcsv-stringify
. - Connect to the database, and change settings to your own.
- Create an empty CSV file.
- Select row-by-row from the database and write to the CSV. Close both the database connection and CSV file when done.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- How To Read CSV Files In NodeJS – Code Boxx
- How To Write CSV Files In NodeJS – Code Boxx
- MySQL – NPMJS
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!