亚洲国产第一_开心网五月色综合亚洲_日本一级特黄特色大片免费观看_久久久久久久久久免观看

Hello! 歡迎來到小浪云!


使用 PHP 自動將 CSV 和 Excel 數據導入 MySQL 和 PostgreSQL 數據庫


avatar
小浪云 2024-11-12 247

使用 PHP 自動將 CSV 和 Excel 數據導入 MySQL 和 PostgreSQL 數據庫

要使用 php 自動將數據從 csv 或 excel 文件傳輸到 mysql 和 postgresql 數據庫,請按照以下步驟操作:

先決條件

  1. 安裝必要的庫:

  2. 下載 phpexcel 庫并將其包含在您的項目目錄中。


第 1 步:設置數據庫連接

我們將使用 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()); } ?> 
登錄后復制

第 2 步:從 csv 或 excel 文件加載數據

我們將創建一個函數來讀取 csv 或 excel 文件并將數據作為數組返回。

<?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");     } } ?> 
登錄后復制

第3步:將數據傳輸到mysql和postgresql

定義函數以將數據插入 mysql 和 postgresql。此示例假設數據是數組的數組,其中每個內部數組代表數據庫中的一行。

<?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>"; } ?> 
登錄后復制

第四步:把它們放在一起

從文件中加載數據,然后將其傳遞給每個函數以插入到 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(); } ?> 
登錄后復制

執行示例

  1. 確保 mysql 和 postgresql 數據庫和表(your_mysql_table、your_pg_table)已設置并具有正確的列(column1、column2、column3)。
  2. 將您的 csv 或 excel 文件放置在指定路徑 ($filepath) 中。
  3. 從命令行或瀏覽器(如果在 web 服務器上)運行此 php 腳本。

此腳本將從指定文件中讀取數據并將其插入到兩個數據庫中。

立即學習PHP免費學習筆記(深入)”;

與我聯系:@ linkedin 并查看我的作品集。

請給我的 github 項目一顆星 ??

相關閱讀