March 27, 2014 9:40 am

How to read Excel file & Insert data into MySQL Database using PHP

I have received many requests from my readers to write tutorial on Excel file import in MySQL database, so today I am going to give you this tutorial on how to read excel file and insert data into MySQL DB using PHP. I have used a php library php-excel-reader its a very simple and easy to understand library to get excel data in your MySQL database. You can also print data in same excel format in HTML and display on browser.


You can get that library from here.

Database Details:

database name => phpgang
table name => excel


Database file run in your MySQL to create database and add data in table.


Edit this file as per your database credentials.


Contains HTML and PHP include library and insert records in database.

First of all it count your sheets in excel file then get rows of each sheet and print rows of each column in html table and insert it in database.

You can get excel_reader2.php by downloading source code.

That’s all for today’s tutorial i hope it helps. Please feel free to give us your feedback in comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:
  • Toon Spin

    This script, besides being not-very-well-written, is dangerous and can destroy your database if someone hands you a malicious Excel file. ALWAYS escape ALL data you enter into a database! For more information read this:

    • huzoorbux

      Thanks for your comment Just edited code.

      • Arun Jishnu

        Notice: Use of undefined constant cells – assumed ‘cells’ in /opt/lampp/htdocs/extras/excel-to-mysql-using-php/index.php on line 19

        • fidanka

          There is error in the syntax [cells], should be [‘cells’]

  • Neesha

    what is the value of cells everywhere in index.php. Could you please tell its significance.

  • prassu

    i have subscribed but still im not able to download

  • shrikant kadam

    i have subscribed….but not able to download this code.. check your database

  • mwashu

    Hi, I have a problem and kindly help ,me. I have created my mysql DB known as mwashu, this DB has 4 tables. the tables names are: 1. blinformation,2. vessel, 3. containers and4. housebl. I want to upload excel file with 4 sheets into the mysql tables. the excel sheets are havinng the same names as my tables’ names. I am having problems. pls i need help on how to have php code upload the excel sheets into my DB tables.
    you can post the code here or you may send me via my email [email protected]
    NB: the excel sheets are large enough

  • Simra Javed

    why it is inserting duplicate rows all the time when i refresh ?

  • how can browse my own file but with same name like yours? but from another location other code directory

  • jeff

    gave your list 2 of my emails, still not able to download! please fix asap and send a note out

  • pandit

    Deprecated: Function split() is deprecated in C:wampwwwpropertyexcel_reader2.php on line 79

    i have got this error plz help thanks for code

  • murali

    i need how to insert word doc data into database using php

  • sivaraman

    nice job i need excel to mysql db using php

  • Subhash Kapil

    when i use this script, it imports only 551 rows at a time to db, how to increase the size of rows so that it would be able to import atleast 1000 rows to db.

    Any help would be greatly appreciated !

  • Nice One.. Itz working
    Is there any for Read data from .xlsx

  • mastermind06

    cells appears to be undefined. how to fix it?
    Please HELP!

    Notice: Use of undefined constant cells – assumed ‘cells’ in F:Php_Codeindex.php on line 16

  • Thank you so much, it’s work but i have received many notification as below:

    Notice: Use of undefined constant cells – assumed ‘cells’ in C:Program Files (x86)EasyPHP-DevServer-14.1VC11datalocalwebexindex.php on line 14

    Can you tell me how to fix?

    • iDontKnow

      instead of writing [cells] write like this [‘cells’];

  • Lidia Stancheva

    I cant insert my data in mysql. Its giving me a lot of errors because of the query. When I write it this way –> $query = “INSERT INTO excel(kurs,potok,grupa,spec,kod,vid,disziplina,zan,prepodavatel,zala,stud,den,ot,do,sedmica) VALUES(‘”.$kurs.”‘,'”.$potok.”‘,'”.$grupa.”‘,'”.$spec.”‘,'”.$kod.”‘,'”.$vid.”‘,'”.$disziplina.”‘,'”.$zan.”‘,'”.$prepodavatel.”‘,'”.$zala.”‘,'”.$stud.”‘,'”.$den.”‘,'”.$ot.”‘,'”.$do.”‘,'”.$sedmica.”‘)”;

    is giving me this error:Parse error: syntax error, unexpected T_VARIABLE

    • Mohanraj

      Use single quotes in values

  • Palmer Kung

    thanks. ^ ^

  • Taushif Ali

    can anybody tell me which functions are used from excel_reader2.php?

  • Taushif Ali

    can anybody tell me which functions are used from excel_reader2.php?

  • thanks suhu , i’ll try it

  • Sam Alexander

    Did nothing but run the example…

    Why does it not work?

    Notice: iconv(): Detected an incomplete multibyte character in input string in /Applications/XAMPP/xamppfiles/htdocs/KazarkDB/excel/excel_reader2.php on line 1718

    Notice: iconv(): Detected an incomplete multibyte character in input string in /Applications/XAMPP/xamppfiles/htdocs/KazarkDB/excel/excel_reader2.php on line 1718

    • sudhir koimattur

      You just have to change the first line of code in index.php to

      SO that it will not display any Notices.

  • Redz

    there is no database file in downloaded file why is it ? please thanks

  • Nizam Uddin GM Sikder

    Hi. I have followed guideline. The code is working nicely in my local machine. However, In production server Its not working. I see no data is added to the database. How to fix this issue? Thanks

  • Rockbaj Indra

    It does not work
    it says ” The filename sample.xls is not readable “.

  • Erwin Justin

    thanks :).. this code worked.. but how about an action from the page to import an excel file.

    • Was you able to connect your php with the google spread sheet on live basis

      • ProgrammingNewbie .

        you have to create web service using Google API to do something like that.

  • Can this work if data is in google spreadsheet and i want to put it in my SQL database.?

  • Jose Angelito Ranojo Diaz

    please help me…
    Deprecated: Assigning the return value of new by reference is deprecated in C:xampphtdocssampleexcel_reader2.php on line 916

    The filename sample.xlsx is not readable

  • Tharunjeni

    How to get name of the sheets using excel_reader2.php library.

  • Milan Gajera

    Thanx for the great tutorial.But it’s notify me see below image

    • shailendra

      these warnings are due to using [cells] without quote so use [‘cells’].

  • shailendra

    My integer value is replacing with ‘GENERAL’ keyword and it comes inside cellinfo array .
    So How I get my actual value on same array ?

  • Eni Chen

    hi, help me

    what the solution for this?

    Parse error: syntax error, unexpected ‘new’ (T_NEW) in C:xampphtdocsexcelexcel-to-mysql-using-phpexcel_reader2.php on line 916

  • Don Zanurano

    can read xlsx?

  • Upender Upendra

    Above example only works for given example excell file only.its not working for new excell file why?

  • Vinay Prabhu

    Hello sir, Thank you for giving a nice tutorial on Excel to mysql

    i have imported this functionality but i am getting error
    Parse error: syntax error, unexpected ‘[‘, expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{‘ or ‘$’ in C:xampphtdocsexcelToDbindex.php on line 28
    for this code => $eid = mysqli_real_escape_string($conn,$data->[$i][‘cells’][$j][1]);

    Please Convey me

    Thank you