To automate data transfer from a CSV or Excel file to both a MySQL and PostgreSQL database using PHP, follow these steps:
Install necessary libraries:
Download the PHPExcel library and include it in your project directory.
We'll use PDO to connect to both MySQL and PostgreSQL.
<?php // MySQL connection $mysqlHost = 'localhost'; $mysqlDB = 'mysql_database'; $mysqlUser = 'mysql_user'; $mysqlPassword = 'mysql_password'; try { $mysqlConnection = new PDO("mysql:host=$mysqlHost;dbname=$mysqlDB", $mysqlUser, $mysqlPassword); $mysqlConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected to MySQL successfully.<br>"; } catch (PDOException $e) { die("MySQL connection failed: " . $e->getMessage()); } // PostgreSQL connection $pgHost = 'localhost'; $pgDB = 'pgsql_database'; $pgUser = 'pgsql_user'; $pgPassword = 'pgsql_password'; try { $pgConnection = new PDO("pgsql:host=$pgHost;dbname=$pgDB", $pgUser, $pgPassword); $pgConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected to PostgreSQL successfully.<br>"; } catch (PDOException $e) { die("PostgreSQL connection failed: " . $e->getMessage()); } ?>
We will create a function that reads either a CSV or Excel file and returns the data as an array.
<?php require 'path/to/PHPExcel.php'; function readFileData($filePath) { $fileType = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); if ($fileType === 'csv') { $data = []; if (($handle = fopen($filePath, 'r')) !== false) { while (($row = fgetcsv($handle, 1000, ',')) !== false) { $data[] = $row; } fclose($handle); } return $data; } elseif ($fileType === 'xls' || $fileType === 'xlsx') { $data = []; $excel = PHPExcel_IOFactory::load($filePath); $sheet = $excel->getActiveSheet(); foreach ($sheet->getRowIterator() as $row) { $rowData = []; $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(false); foreach ($cellIterator as $cell) { $rowData[] = $cell->getValue(); } $data[] = $rowData; } return $data; } else { throw new Exception("Unsupported file format"); } } ?>
Define functions to insert data into both MySQL and PostgreSQL. This example assumes the data is an array of arrays, where each inner array represents a row in the database.
<?php function insertIntoMySQL($mysqlConnection, $data) { $query = "INSERT INTO your_mysql_table (column1, column2, column3) VALUES (?, ?, ?)"; $stmt = $mysqlConnection->prepare($query); foreach ($data as $row) { $stmt->execute($row); } echo "Data inserted into MySQL successfully.<br>"; } function insertIntoPostgreSQL($pgConnection, $data) { $query = "INSERT INTO your_pg_table (column1, column2, column3) VALUES (?, ?, ?)"; $stmt = $pgConnection->prepare($query); foreach ($data as $row) { $stmt->execute($row); } echo "Data inserted into PostgreSQL successfully.<br>"; } ?>
Load data from the file, then pass it to each function to insert into both MySQL and PostgreSQL.
<?php $filePath = 'path/to/yourfile.csv'; // or .xls / .xlsx try { $data = readFileData($filePath); insertIntoMySQL($mysqlConnection, $data); insertIntoPostgreSQL($pgConnection, $data); } catch (Exception $e) { echo "Error: " . $e->getMessage(); } ?>
This script will read the data from the specified file and insert it into both databases.
Connect with me:@ LinkedIn and checkout my Portfolio.
Please give my GitHub Projects a star ⭐️
The above is the detailed content of Automated CSV and Excel Data Import to MySQL and PostgreSQL Databases Using PHP. For more information, please follow other related articles on the PHP Chinese website!