I need to upload csv and import this data to MySQL table.
public function insertData()
{
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$query = "INSERT into deleted_users (email)
values ('" . $column[0] . "'); $query->execute();
}
}
Perhaps the fastest way to bulk load data from PHP into MySQL is to use the LOAD DATA
tool, something like this:
$sql = "LOAD DATA LOCAL INFILE 'path/to/yourfile.csv'
INTO TABLE deleted_users
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '
'
(@col1)
SET email = @col1;";
mysqli_query($conn, $sql);
This answer loads records with only the email
field being assigned. If you want to assign a value for the id
and type
columns, then you will need a CSV file containing these values.