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` ( `id` int(11) NOT NULL AUTO_INCREMENT, `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', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
step 2: Database Configuration
connect_db.php
die("Connection failed: " . $db->connect_error); }
step 3: CSV File Upload
index.php
if($result->num_rows > 0){while($row = $result->fetch_assoc()){?>
#ID Name Mobile Status No member(s) found...
step 4: Import CSV Data to Database
importData.php
$db->query("UPDATE friends SET name = '".$name."', mobile = '".$mobile."', is_active = '".$is_active."', updated_at = NOW() WHERE email_address = '".$email_address."'"); }else{ $db->query("INSERT INTO friends (name, email_address, mobile, join_at, updated_at, is_active) VALUES ('".$name."', '".$email_address."', '".$mobile."', NOW(), NOW(), '".$is_active."')"); } } fclose($csvFile); $qstring = '?is_active=succ'; }else{ $qstring = '?is_active=err'; } }else{ $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 infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.