How to Import CSV Into MySQL Database With PHP

If you’re a PHP developer, then you might have faced the issue while importing CSV to MySQL  Database, right? Importing CSV data and inserting them into MySQL database? For instance, you have a CSV file containing hundreds of values and you need to import CSV into MySQL database. Now obviously you can’t copy each data and insert it manually. What you need is a better and quicker solution for this. The solution that doesn’t consume much time.

To overcome this problem, we have the right solution. Here, in this PHP tutorial, we’ll show how you can MySQL import CSV into a table into your MySQL database.

Let’s Get Started

Create a file with index.php for HTML inputs.

First create a form.

<title>infoblogging => Upload CSV and Insert into Database Using PHP</title>
<form method='POST' enctype='multipart/form-data'>
Upload CSV FILE: <input type='file' name='csv_info' /> <input type='submit' name='submit' value='Upload File' />

In this form, define Encrypt method because when the user will upload any image, then “multipart/form-data” method will also compulsory to define.

<form method='POST' enctype='multipart/form-data'>

Now create a database name as “csvdata” and create a new database and write following code.

CREATE TABLE `tbl_csv` (
`item1` VARCHAR(50) NOT NULL,
`item2` VARCHAR(50) NOT NULL,

After creating database, next step is to create a file and name it as “connection.php” and write following code into it.

$servername = "localhost"; $username = "root"; $password = "ur48x";
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

next, we’ll select the database. write following code for it.


NOTE :- Here csvdata is our database Name.

Now open “index.php” file and add the following string.

include_once 'connection.php';

Finally, we’ll validate the csv file and then import its records in database.


$arrFileName = explode('.',$_FILES['csv_data']['name']);
if($arrFileName[1] == 'csv'){
$handle = fopen($_FILES['csv_data']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

$item1 = mysqli_real_escape_string($conn,$data[0]);
$item2 = mysqli_real_escape_string($conn,$data[1]);
$import="INSERT into tbl_csv(item1,item2) values('$item1','$item2')";
print "Import done";

And done!

Want to Create a Web Application?

Validate your app idea and get a free quote.

Using this method, we can import large CSV file into MySQL Database easily and quickly. So next time, when you require assistance to import CSV into MySQL or PHP upload CSV file while building your business website, you can either use this method or Hire PHP developer to do the job for you.

Grab a free copy of Import CSV PHP tutorial demo from Github.

Author Bio
Jigar Mistry
Jigar Mistry
Designation: Vice President (Technologies)

Jigar Mistry is Vice President (Technologies) at Space-O Technologies. He has 14+ years of experience in the web and mobile app development industry. He has expertise in different mobile app categories like health and fitness, eCommerce, and on-demand.