import data from excel to mysql using php code – How to Import Excel/CSV file to MySQL Database using PHP?

import data from excel to mysql using php code – import excel file into mysql database tutorial first of all Install PHPExcel with Composer.

import data from excel to mysql using php code

learn import excel file into mysql table with complete source code and explanation. also you can learn to Upload a CSV(excel) file and import into database using PHP and MYSQL.

1.Download Package

So first let’s download from here :

Click Here to download PHP Excel

2.Create connect_db.php file

	$dbHost = "localhost";
	$dbDatabase = "pakainfo_v1";
	$dbPasswrod = "SD#$%@5665";
	$dbUser = "root";
	$mysqli = new mysqli($dbHost, $dbUser, $dbPasswrod, $dbDatabase);

3.Create index.php file

Therefor let’s copy bellow source code as well as put it into index.php file.

<!DOCTYPE html>
	<title>Excel Uploading PHP -</title>
	<link rel="stylesheet" type="text/css" href="">

<div class="container">
	<h2>Excel Upload - File</h2>

	<form method="POST" action="fileStoreNew.php" enctype="multipart/form-data">
		<div class="form-group">
			<label>Upload Excel File</label>
			<input type="file" name="file" class="form-control">
		<div class="form-group">
			<button type="submit" name="Submit" class="btn btn-success">Upload</button>
		<p>Download Demo File from here : <a href="example.ods"><strong>example.ods</strong></a></p>


Don’t Miss : import excel file into mysql database tutorial

Also Read This πŸ‘‰   PHP Symmetric array destructuring with Associative

4.Create Upload Folder

So, let’s create fileStoreNew.php file and put bellow code:



  $mimes = ['application/','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];

    $uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
    $Reader = new SpreadsheetReader($uploadFilePath);
    $countAllData = count($Reader->sheets());

    echo "You have total ".$countAllData." sheets".

    $table_view="<table border='1'>";

    /* For Loop for all sheets */


      foreach ($Reader as $dataResult)
        $title = isset($dataResult[0]) ? $dataResult[0] : '';
        $livecomments = isset($dataResult[1]) ? $dataResult[1] : '';
        $query = "insert into items(title,livecomments) values('".$title."','".$livecomments."')";


    echo $table_view;
    echo "<br />Data Inserted in MySQL dababase";

  }else { 
    die("<br/>Sorry, File type is not allowed. Only Excel file."); 



I hope you get an idea about import data from excel to mysql using php code.
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.