Last active
September 1, 2019 07:59
-
-
Save msamgan/798363dbe026e546400f1b6b5e06c666 to your computer and use it in GitHub Desktop.
CSV Importer in PHP ( run via terminal)
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 | |
/** | |
* importer for CSV in MySQL DB. | |
* pleas keep tje CSV file in the same directory and run : | |
* | |
* > php ./import.php | |
*/ | |
$host = "localhost"; // Host name. | |
$db_user = "root"; //mysql user | |
$db_password = ""; //mysql pass | |
$db = ''; // Database name. | |
$con = mysqli_connect($host, $db_user, $db_password, $db); | |
// Check connection | |
if (mysqli_connect_errno()) { | |
echo "Failed to connect to MySQL: " . mysqli_connect_error(); | |
} | |
$file = fopen('filename.extension', "r"); | |
while (($data = fgetcsv($file, 100000, ",")) !== FALSE) { | |
$sql = "INSERT into hs_codes(col1, col2, col3) values('$data[0]', '$data[1]', '$data[2]')"; | |
mysqli_query($con, $sql); | |
} | |
fclose($file); | |
echo "CSV File has been successfully Imported."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment