Import CSV file data into MySQL database using PHP code example
Example 1: how to export php mysql data to csv through php
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "test";
$con = new mysqli($host, $user, $password, $database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
Example 2: import data from csv to db php
<?php
$query = <<<eof
LOAD DATA INFILE '$fileName'
INTO TABLE tableName
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(field1,field2,field3,etc)
eof;
$db->query($query);
?>