php import csv to mysql – Import CSV File Data into MySQL Database using PHP

php import csv to mysql: Import CSV File into MySQL using PHP.Create a Database in MySQL – Create MySql Connection in PHP – Import CSV to MySQL in PHP – Display the Saved Records – Export MySQL to CSV With PHP.

php import csv to mysql

List of the Steps to import CSV file into MySql database using PHP.

step 1: Create Database Table

CREATE TABLE `friends` (
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`email_address` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`mobile` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`join_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`is_active` enum('Active','Inactive') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

step 2: Database Configuration


 die("Connection failed: " . $db->connect_error);

step 3: CSV File Upload


if($result->num_rows > 0){while($row = $result->fetch_assoc()){?>
#ID Name Email Mobile Status
No member(s) found...

step 4: Import CSV Data to Database


 $db->query("UPDATE friends SET name = '".$name."', mobile = '".$mobile."', is_active = '".$is_active."', updated_at = NOW() WHERE email_address = '".$email_address."'");
$db->query("INSERT INTO friends (name, email_address, mobile, join_at, updated_at, is_active) VALUES ('".$name."', '".$email_address."', '".$mobile."', NOW(), NOW(), '".$is_active."')");


$qstring = '?is_active=succ';
$qstring = '?is_active=err';
$qstring = '?is_active=invalid_file';

header("Location: index.php".$qstring);

Don’t Miss: upload csv file in php

I hope you get an idea about php import csv to mysql.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment