Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:19
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/fea19a494dc00e858737ab7e72e7863d to your computer and use it in GitHub Desktop.
Save code-boxx/fea19a494dc00e858737ab7e72e7863d to your computer and use it in GitHub Desktop.
PHP Import Excel Into MYSQL

IMPORT EXCEL INTO MYSQL

https://code-boxx.com/import-excel-into-mysql-php/

NOTES

  1. GIST does not allow XLSX files. Convert 2b-dummy.csv to 2b-dummy.xlsx on your own, or generate your own dummy data.
  2. A copy of PHPSpreadsheet is not included, please download your own - composer require phpoffice/phpspreadsheet.
  3. Create a test database and import 2a-dummy.sql.
  4. Change the database settings in 3-import.php to your own, then launch it in the browser.

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.

1) Download and install Composer - https://getcomposer.org/
2) Open command line, navigate to project folder - cd MY/HTTP/.
3) Run "composer require phpoffice/phpspreadsheet"
Done - Composer will download the latest version to the vendor/ folder.
PHPSpreadsheet - https://github.com/PHPOffice/PhpSpreadsheet
CREATE TABLE `users` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
ALTER TABLE `users`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
John Doe john@doe.com
Jane Doe jane@doe.com
Josh Doe josh@doe.com
Joy Doe joy@doe.com
Janus Doe janus@doe.com
Jay Doe jay@doe.com
June Doe june@doe.com
Julius Doe julius@doe.com
Jess Doe jess@doe.com
Jack Doe jack@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;charset=$dbchar;dbname=$dbname",
$dbuser, $dbpass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (B) PHPSPREADSHEET TO LOAD EXCEL FILE
require "vendor/autoload.php";
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load("2b-dummy.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
// (C) READ DATA + IMPORT
$sql = "INSERT INTO `users` (`name`, `email`) VALUES (?,?)";
foreach ($worksheet->getRowIterator() as $row) {
// (C1) FETCH DATA FROM WORKSHEET
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) { $data[] = $cell->getValue(); }
// (C2) INSERT INTO DATABASE
print_r($data);
try {
$stmt = $pdo->prepare($sql);
$stmt->execute($data);
echo "OK - USER ID - {$pdo->lastInsertId()}<br>";
} catch (Exception $ex) { echo $ex->getMessage() . "<br>"; }
$stmt = null;
}
// (D) CLOSE DATABASE CONNECTION
if ($stmt !== null) { $stmt = null; }
if ($pdo !== null) { $pdo = null; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment