Import database from excel sheet to PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gemini1247
    New Member
    • Jan 2007
    • 1

    Import database from excel sheet to PHP

    Can any one help in importing database from excel sheet to PHP?
    Bye,
    Sound.
  • subash
    New Member
    • Sep 2006
    • 32

    #2
    Hi,

    If your excel file is in CSV format you can do the following

    1. In PHP you can use fgetcsv command to process and try insert into mysql on help of mysql commands

    2.The best method is try using the following mysql statement

    [PHP]
    LOAD DATA LOCAL INFILE '/importfile.csv'
    INTO TABLE test_table
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
    (field1, filed2, field3);
    [/PHP]


    Subash :)

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by gemini1247
      Can any one help in importing database from excel sheet to PHP?
      Bye,
      Sound.
      Do you want to import a database FROM an Excel sheet INTO PHP? Obviously not. So it is either:

      - import an Excel sheet (not all cells) into a MySQL database table using PHP
      - import an Excel sheet (all cells) into a MySQL database table using LOADFILE
      - export a MySQL table into an Excel XML sheet using PHP.

      Which one is it?

      Ronald :cool:

      Comment

      • ergocho
        New Member
        • Feb 2007
        • 2

        #4
        If you want to import an exel sheet with php to your database, you must have sheet by sheet save as csv and then you can use this ex code


        [PHP]<?php
        //reading csv file
        $fname="file.cs v";
        $fp=fopen($fnam e,"r") or die("csv Error");
        $line = fgets( $fp, 2024 );
        //conexion with db started
        $c=mysql_connec t("localhost"," root","");
        if(!$c)
        {
        die("Not Conected");
        }
        if(!mysql_selec t_db("dbspl"))
        {
        die("Table Error");
        }

        while(!feof($fp ))
        {
        list($var1,$var 2,$var3) =split( ",", $line); //list of variables to import
        $line = fgets( $fp, 2024 );
        $ctd=$ctd+1;
        if($ctd > 1)
        {
        $var1=strtouppe r($var1);//Uppercase

        if($loca!="" && $pudoc!="")
        {
        if(!($inserc=my sql_query("INSE RT INTO table (field1,field2, field3)
        VALUES('$var1', '$var2','$var3' )")))
        {
        echo'<center>';
        echo'<br>';
        echo mysql_error();
        echo'<br>';
        echo"User Inser Error";
        echo'</center>';
        }
        }//
        }// if($ctd > 1)
        } // while(!feof($fp ))
        fclose($fp);
        ?> [/PHP]

        Comment

        Working...