要使用 php 自動(dòng)將數(shù)據(jù)從 csv 或 excel 文件傳輸?shù)?mysql 和 postgresql 數(shù)據(jù)庫(kù),請(qǐng)按照以下步驟操作:
先決條件
-
安裝必要的庫(kù):
-
下載 phpexcel 庫(kù)并將其包含在您的項(xiàng)目目錄中。
第 1 步:設(shè)置數(shù)據(jù)庫(kù)連接
我們將使用 pdo 連接到 mysql 和 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()); } ?>
登錄后復(fù)制
第 2 步:從 csv 或 excel 文件加載數(shù)據(jù)
我們將創(chuàng)建一個(gè)函數(shù)來(lái)讀取 csv 或 excel 文件并將數(shù)據(jù)作為數(shù)組返回。
<?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"); } } ?>
登錄后復(fù)制
第3步:將數(shù)據(jù)傳輸?shù)絤ysql和postgresql
定義函數(shù)以將數(shù)據(jù)插入 mysql 和 postgresql。此示例假設(shè)數(shù)據(jù)是數(shù)組的數(shù)組,其中每個(gè)內(nèi)部數(shù)組代表數(shù)據(jù)庫(kù)中的一行。
<?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>"; } ?>
登錄后復(fù)制
第四步:把它們放在一起
從文件中加載數(shù)據(jù),然后將其傳遞給每個(gè)函數(shù)以插入到 mysql 和 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(); } ?>
登錄后復(fù)制
執(zhí)行示例
- 確保 mysql 和 postgresql 數(shù)據(jù)庫(kù)和表(your_mysql_table、your_pg_table)已設(shè)置并具有正確的列(column1、column2、column3)。
- 將您的 csv 或 excel 文件放置在指定路徑 ($filepath) 中。
- 從命令行或?yàn)g覽器(如果在 web 服務(wù)器上)運(yùn)行此 php 腳本。
此腳本將從指定文件中讀取數(shù)據(jù)并將其插入到兩個(gè)數(shù)據(jù)庫(kù)中。
立即學(xué)習(xí)“PHP免費(fèi)學(xué)習(xí)筆記(深入)”;
與我聯(lián)系:@ linkedin 并查看我的作品集。
請(qǐng)給我的 github 項(xiàng)目一顆星 ??