---
description: >-
  https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/
---

# PHP MySQLi How To Save CSV Data To Database Table Tutorial

## PHP MySQLi How To Save CSV Data To Database Table Tutorial

June 18, 2022 by [Sanjay Kumar](https://onlinewebtutorblog.com/author/admin/)

Table of Contents

* [Create Database & Table](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Create_Database_Table)
* [Dummy Data for Application](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Dummy_Data_for_Application)
* [Application Folder Structure](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Application_Folder_Structure)
* [Database Configuration](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Database_Configuration)
* [Application Programming](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Application_Programming)
* [Application Testing](https://onlinewebtutorblog.com/php-mysqli-how-to-save-csv-data-to-database-table-tutorial/#Application_Testing)

Share this Article

*
*
*
*
*

Reading Time: 6 minutes 2 Views

Inside this article we will see the concept i.e PHP MySQLi How To Save CSV file to Database. Article contains classified information. It will give the complete idea of parsing a CSV file and save into PHP MySQLi database.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you will learn only reading CSV file, you can use the same concept in data seeding to database via CSV file. This is step by step tutorial in PHP about CSV file data seeding inside mysql database.

Learn More –

* [PHP MySQLi Dynamic Dependent Dropdown using jQuery Ajax](https://onlinewebtutorblog.com/php-mysqli-dynamic-dependent-dropdown-using-jquery-ajax/)
* [PHP MySQLi How To Call Stored Procedure Tutorial](https://onlinewebtutorblog.com/php-mysqli-how-to-call-stored-procedure-tutorial/)
* [PHP MySQLi jQuery UI Autocomplete Database Search](https://onlinewebtutorblog.com/php-mysqli-jquery-ui-autocomplete-database-search/)
* [How To Generate QR Code in PHP Using Library Tutorial](https://onlinewebtutorblog.com/how-to-generate-qr-code-in-php-using-library-tutorial/)

Let’s get started.

### Create Database & Table

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

```
CREATE DATABASE php_applications;
```

Inside this database, we need to create a table.

Tables we need – **students**

```
CREATE TABLE `students` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(150) NOT NULL,
 `email` varchar(50) NOT NULL,
 `gender` enum('male','female','others') NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
```

### Dummy Data for Application

Here, you need to copy mysql query from this given file and run into **sql tab** of phpmyadmin. First download it and run.

[Download students.csv](https://onlinewebtutorblog.com/wp-content/uploads/2022/06/students.csv)

We will use this csv data and insert into students table. You need to put this downloaded file into your application setup.

### Application Folder Structure

You need to create a folder structure to develop this application in PHP and MySQLi. Have a look the files and folders inside this application –

Create a folder with name **php-mysqli-csv** and create these **2 files** into it.

![](https://onlinewebtutorblog.com/wp-content/uploads/2022/06/php-mysqli-save-csv-data-to-database.png)

### Database Configuration

Open **dbconfig.php** file from folder. Add these lines of code into it.

dbconfig.php

```
<?php
```

```
/*
```

```
 @Author: Sanjay Kumar
```

```
 @Project: PHP & MySQLi How To Save CSV Data To Database Tutorial
```

```
 @Email: onlinewebtutorhub@gmail.com
```

```
 @Website: https://onlinewebtutorblog.com/
```

```
*/
```

```
​
```

```
// Database configuration
```

```
$host   = "localhost";
```

```
$dbuser = "admin";
```

```
$dbpass = "Admin@123";
```

```
$dbname = "php_applications";
```

```
​
```

```
// Create database connection
```

```
$conn = new mysqli($host, $dbuser, $dbpass, $dbname);
```

```
​
```

```
// Check connection
```

```
if ($conn->connect_error) {
```

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

```
}
```

```
​
```

### Application Programming

Open **index.php** file and write this following code into it.

* ![](https://assets.vlitag.com/widget/2020/06/22/1592801729.jpg)![](https://assets.vlitag.com/widget/2019/11/05/1572962870.jpg)![](https://assets.vlitag.com/widget/2019/11/05/1572962830.jpg)![](https://assets.vlitag.com/widget/2020/07/30/1596163502.jpg)![](https://assets.vlitag.com/widget/2022/03/31/1648753746.png)![](https://assets.vlitag.com/widget/2022/03/31/1648753124.png)![](https://assets.vlitag.com/widget/2019/11/05/1572962830.jpg)![](https://assets.vlitag.com/widget/2020/06/22/1592801729.jpg)![](https://assets.vlitag.com/widget/2019/11/05/1572962870.jpg)

![](https://assets.vlitag.com/widget/2019/11/05/1572962830.jpg)index.php

```
<?php
```

```
/*
```

```
 @Author: Sanjay Kumar
```

```
 @Project: PHP & MySQLi How To Save CSV Data To Database Tutorial
```

```
 @Email: onlinewebtutorhub@gmail.com
```

```
 @Website: https://onlinewebtutorblog.com/
```

```
*/
```

```
​
```

```
// Include the database file
```

```
require 'dbconfig.php';
```

```
​
```

```
$students = [];
```

```
​
```

```
if (($open = fopen("students.csv", "r")) !== FALSE) {
```

```
​
```

```
    while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
```

```
        $students[] = $data;
```

```
    }
```

```
​
```

```
    fclose($open);
```

```
}
```

```
​
```

```
echo "<pre>";
```

```
print_r($students);
```

```
​
```

If you print the above output, you should like this –

![](https://onlinewebtutorblog.com/wp-content/uploads/2022/06/how-to-print-csv-data-php-mysqli.png)

Let’s save this array of data into database.

Here, is the complete code of **index.php**

index.php

```
<?php
```

```
/*
```

```
 @Author: Sanjay Kumar
```

```
 @Project: PHP & MySQLi How To Save CSV Data To Database Tutorial
```

```
 @Email: onlinewebtutorhub@gmail.com
```

```
 @Website: https://onlinewebtutorblog.com/
```

```
*/
```

```
​
```

```
// Include the database file
```

```
require 'dbconfig.php';
```

```
​
```

```
$students = [];
```

```
​
```

```
if (($open = fopen("students.csv", "r")) !== FALSE) {
```

```
​
```

```
    while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
```

```
        $students[] = $data;
```

```
    }
```

```
​
```

```
    fclose($open);
```

```
}
```

```
​
```

```
// echo "<pre>";
```

```
// print_r($students);
```

```
​
```

```
if (!empty($students) && count($students) > 0) {
```

```
​
```

```
    foreach ($students as $index => $student) {
```

```
​
```

```
        if ($index == 0) {
```

```
            continue;
```

```
        }
```

```
​
```

```
        $stmt = $conn->prepare("INSERT INTO students (name, email, gender) VALUES(?, ?, ?)");
```

```
        $stmt->bind_param("sss", $student[0], $student[1], $student[2]);
```

```
        $stmt->execute();
```

```
    }
```

```
}
```

```
​
```

### Application Testing

Now,

URL: http://localhost/php-mysqli-csv/index.php

![](https://onlinewebtutorblog.com/wp-content/uploads/2022/06/php-mysqli-how-to-save-csv-data-to-database-table.png)

Download Complete Source Code

[Download Source Code](https://github.com/owthub/articles/tree/master/php-mysqli-csv)

We hope this article helped you to PHP MySQLi How To Save CSV Data To Database Table Tutorial in a very detailed way.

[Buy Me a Coffee](https://www.buymeacoffee.com/onlinewebtutor)

Online Web Tutor invites you to try [**Skillshare**](https://www.skillshare.com/r/user/onlinewebtutor?gr_tch_ref=on) free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click [**here**](https://www.skillshare.com/r/user/onlinewebtutor?gr_tch_ref=on) to join.

If you liked this article, then please subscribe to our [**YouTube Channel**](https://www.youtube.com/onlinewebtutor?sub_confirmation=1) for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on [**Twitter**](https://twitter.com/owthub) and [**Facebook**](https://www.facebook.com/owthub).
