Export .xls to mysql table..

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

Export .xls to mysql table..

Post 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..
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Export .xls to mysql table..

Post 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.
Post Reply