Created
April 27, 2019 16:09
-
-
Save groundcat/624a132ef80e984512c2c59378128a18 to your computer and use it in GitHub Desktop.
Import CSV to MySQL by PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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