Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export simple excel data into MySQL using PHP

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 ?

like image 228
ramesh Avatar asked Dec 07 '22 11:12

ramesh


2 Answers

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);
like image 152
Sohail Ahmed Avatar answered Dec 09 '22 01:12

Sohail Ahmed


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().

like image 35
slash28cu Avatar answered Dec 09 '22 00:12

slash28cu