Export simple excel data into MySQL using PHP

12,683

Solution 1

Save this excel file as csv and run the following code with add your changings

$source = fopen('email.csv', 'r') or die("Problem open file");
    while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
    {
        $name = $data[0];
        $email = $data[1];


        mysql_query("INSERT INTO `table` (`name`,`email`) VALUES ('".$name."','".$email."') ");


    }
    fclose($source);

Solution 2

There is a Iibrary named PHPExcel. With this library you can easily parse any excel file. Or you can export your file as csv and will be easier for you. php has native functions to handle csv files. You can use fgetcsv() or str_getcsv().

Solution 3

Goto this link and download the php class that will read the excel file and return the array. This array will hold the all data written in excel file.

php excel reader

Its free...

You can also see the demo there.

I am already using it. Its definitely good.

Any other help on this you can freely ask to me.

Share:
12,683

Related videos on Youtube

ramesh
Author by

ramesh

Updated on June 17, 2022

Comments

  • ramesh
    ramesh almost 2 years

    My client got a excel file with the following structure

    name     |     email
    ----------------------------
    Name     |   email here
    Name     |   email here
    Name     |   email here
    Name     |   email here
    Name     |   email here
    Name     |   email here
    

    I would likes to make a MySQL database table according to this pattern and save the data into MySQL.

    I am wonder how to do this. Also there is an option needed

    We have to check that if that corresponding user had a correct email address, ie of the form @ .

    Can we check the data as a loop while importing ?

    Also how to convert this data to MySQL ?

    • Asad Saeeduddin
      Asad Saeeduddin over 11 years
      Is it feasible to convert to CSV first?
    • GregD
      GregD
      As @Asad said before, first convert to CSV and then use mysql to import the file into the database.
  • ramesh
    ramesh over 11 years
    is there any file size limitation ?
  • Mark Baker
    Mark Baker over 11 years
    Except the limitation of PHP memory
  • Jestin
    Jestin about 4 years
    With this code, I am able to get only the first row of the file.