Page 1 of 1

Export .xls to mysql table..

Posted: Mon Jul 28, 2008 1:04 pm
by pavanpuligandla
Hii,,
i'm trying to export an excel workbook to mysql table but couldnt succeed, at last i found a solution which is not that much perfect to implement( i,e import CSV to Mysql table.)
I want to export any xls file to the specified database table, but in my code i'm predefining the .csv file which is to be exported to the mysql table..
kindly go thru this and do the need ful.. the code is as follows :;

Code: Select all

<?PHP
 
//Connect to mysql server
    $link=mysql_connect("localhost","root","");
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
    //Select database
    $db=mysql_select_db("college");
    if(!$db) {
        die("Unable to select database");
    }
 
$fcontents = file ('http://localhost/Project/spsheet.xls');
          # expects the csv file to be in the same dir as this script
      
             for($i=0; $i<sizeof($fcontents); $i++) {
             $line = trim($fcontents[$i]);
             echo "$line<BR>";
            $arr = explode("','", $line);
             echo "$arr";
             #if your data is comma separated
             # instead of tab separated,
             # change the '\t' above to ','    
             $sql = "insert into subject values (". implode("'", $arr).")";
             #$sql = "insert into test values ('". $arr ."')";
             mysql_query($sql);
             echo $sql ."<br>\n";
             if(mysql_error()) {
            echo mysql_error() ."<br>\n";
             }
        }
      ?>
my motto is to implement the code which it should read the contents of input <b>ANY</b> .xls file instead of predefining the path to only a selected single .xls file...

[$fcontents = file ('http://localhost/Project/spsheet.xls');]-- predefined path of xls file..

Re: Export .xls to mysql table..

Posted: Mon Jul 28, 2008 1:47 pm
by ghurtado
This is in no way related to "php security" - why did you post it here? Also, please do not cross-post, it is considered bad manners and it will make it much less likely that someone will want to help you.