php import csv to mysql – There are many ways to read CSV files but in this Article, i will use 2 main functions to read CSV files fopen() and fgetcsv() Example with demo.
Import CSV to MySQL phpMyAdmin
Import CSV to MySQL phpMyAdmin. it reads the uploaded CSV file and parses the data.
step 1 : Create Database Table
created in the database
CREATE TABLE `players` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `mobile` varchar(15) COLLATE utf8_unicode_ci NOT NULL, `created` 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
dbConfig.php
connect_error) { die("Connection failed: " . $db->connect_error); }
step 3 : CSV File Upload
index.php
query("SELECT * FROM players ORDER BY id DESC"); if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ ?> #ID Name mobile is_active No member(s) found...
Added Bootstrap library
Import CSV Data to Database
importData.php
query($prevQuery); if($prevResult->num_rows > 0){ $db->query("UPDATE players SET name = '".$name."', mobile = '".$mobile."', is_active = '".$is_active."', updated_at = NOW() WHERE email = '".$email."'"); }else{ $db->query("INSERT INTO players (name, email, mobile, created, updated_at, is_active) VALUES ('".$name."', '".$email."', '".$mobile."', NOW(), NOW(), '".$is_active."')"); } } fclose($csvFile); $quser_line = '?is_active=succ'; }else{ $quser_line = '?is_active=err'; } }else{ $quser_line = '?is_active=invalid_file'; } } header("Location: index.php".$quser_line);
I hope you get an idea about Import CSV to MySQL phpMyAdmin.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.