Skip to content

Instantly share code, notes, and snippets.

@groundcat
Created April 27, 2019 16:09
Show Gist options
  • Save groundcat/624a132ef80e984512c2c59378128a18 to your computer and use it in GitHub Desktop.
Save groundcat/624a132ef80e984512c2c59378128a18 to your computer and use it in GitHub Desktop.
Import CSV to MySQL by PHP
<?php
// Debug
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Connect
$conn = mysqli_connect("IP", "USERNAME", "PASSWORD", "DBNAME");
// CSV File
$fileName = "CSV_FILE_PATH";
$file = fopen($fileName, "r");
//Import to MySQL
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$sqlInsert = "INSERT into DBNAME (Name, ID, DescriPt)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
echo "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
echo "error";
$message = "Problem in Importing CSV Data";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment