import excel data into mysql using php script ? problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meeanji
    New Member
    • May 2008
    • 7

    import excel data into mysql using php script ? problem

    php codes goes like this[php]<?
    $fcontents = file ('./spreadsheet.xls ');
    # expects the csv file to be in the same dir as this script
    for($i=0; $i<sizeof($fcon tents); $i++) {
    $line = trim($fcontents[$i]);
    $arr = explode("\t", $line);
    $sql = "insert into TABLENAME values ('".
    implode("','", $arr) ."')";
    mysql_query($sq l);
    echo $sql ."<br>\n";
    if(mysql_error( )) {
    echo mysql_error() ."<br>\n";
    }
    }
    ?>[/php]// above is my code . it is working fine for comma separated values.But i am unable to work out with tab . and is not working for XLS file. my aim is to read Excel data into Mysql database.

    thanks,
    Anji
    Last edited by ronverdonk; May 9 '08, 08:30 AM. Reason: warning: use code tags!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    WARNING:
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      I think it is this line [PHP]$sql = "insert into TABLENAME values ('".
      implode("','", $arr) ."')"; [/PHP] You are looking for commas.
      If it is tab delimted you need [PHP]$sql = "insert into TABLENAME values ('".
      implode("\t", $arr) ."')"; [/PHP]

      Comment

      • yuva17raj
        New Member
        • Dec 2007
        • 19

        #4
        hi
        with the same code is it possible to update the database table with excel file base on some condition
        such as

        update tablename set field1 = '$exceldata1' where field2 = '$exceldata2'


        if soo help me with the sample code

        regards
        yuvaraj

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by yuva17raj
          with the same code is it possible to update the database table with excel file base on some condition such as
          update tablename set field1 = '$exceldata1' where field2 = '$exceldata2'
          yes, I would it do exactly this way

          Comment

          Working...