我需要上传csv并将此数据导入MySQL表。
public function insertData()
{
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$query = "INSERT into deleted_users (email)
values ('" . $column[0] . "'); $query->execute();
}
}
答案 0 :(得分:0)
也许将数据从PHP批量加载到MySQL的最快方法是使用LOAD DATA
工具,如下所示:
$sql = "LOAD DATA LOCAL INFILE 'path/to/yourfile.csv'
INTO TABLE deleted_users
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
(@col1)
SET email = @col1;";
mysqli_query($conn, $sql);
此答案加载仅分配了email
字段的记录。如果要为id
和type
列分配值,则需要一个包含这些值的CSV文件。