shane07 Posted January 18, 2008 Share Posted January 18, 2008 Hello to all Plz somebody help me with reading excel sheet and storing the data in the mysql table. Waiting for your reply. Thanx Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/ Share on other sites More sharing options...
vbnullchar Posted January 18, 2008 Share Posted January 18, 2008 Simply export data from excel as TXT or CSV. then LOAD DATA INFILE 'path/file.csv' INTO TABLE your_table; Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442584 Share on other sites More sharing options...
shane07 Posted January 18, 2008 Author Share Posted January 18, 2008 Simply export data from excel as TXT or CSV. then LOAD DATA INFILE 'path/file.csv' INTO TABLE your_table; Is that the sql statement that can be used in php script? Thank you for ur reply but what should I do if I have more than two tables referenced to same excel sheet Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442586 Share on other sites More sharing options...
pdkv2 Posted January 18, 2008 Share Posted January 18, 2008 use php pear classes for that or else if you are using this feature on windows you can create the datasource with excel and query the excel sheet to retrieve the values. Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442601 Share on other sites More sharing options...
shane07 Posted January 18, 2008 Author Share Posted January 18, 2008 use php pear classes for that or else if you are using this feature on windows you can create the datasource with excel and query the excel sheet to retrieve the values. Thank you I m using windows for testing. But using linux for online testing. Does the php pear class is supported by both windows and linux? Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442613 Share on other sites More sharing options...
pdkv2 Posted January 18, 2008 Share Posted January 18, 2008 Yes you can use php pear classes across the platforms Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442615 Share on other sites More sharing options...
shane07 Posted January 18, 2008 Author Share Posted January 18, 2008 Yes you can use php pear classes across the platforms I searched for that class in pear.php.net but only found spreadsheet excel writer. Actually I have never used pear classes before. Can u suggest me more on this class? Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442616 Share on other sites More sharing options...
ratcateme Posted January 18, 2008 Share Posted January 18, 2008 try saving your excel sheet in txt format then writing php to insert it into your database Scott. Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442632 Share on other sites More sharing options...
ratcateme Posted January 18, 2008 Share Posted January 18, 2008 I haven't tested this but it should work for a sheet saved in txt format <pre> <?php $con=mysql_connect(); mysql_select_db('test'); $sheet='test.txt'; $file=fopen($sheet,'r'); while(!feof($sheet)){ $line=fgets($sheet); $line=explode("\t",$line); $query='INSERT INTO `table` VALUES ('; foreach($line as $key => &$value){ if($$key==0){ $query.="'".$value."'"; }else{ $query.=", '".$value."'"; } } $query.=');'; echo $query."\n"; mysql_query($query,$con); } fclose($file); ?> </pre> Or if you save it as CSV change the explode from "\t" to "," Hope this helps. Scott. Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442637 Share on other sites More sharing options...
shane07 Posted January 18, 2008 Author Share Posted January 18, 2008 I haven't tested this but it should work for a sheet saved in txt format <pre> <?php $con=mysql_connect(); mysql_select_db('test'); $sheet='test.txt'; $file=fopen($sheet,'r'); while(!feof($sheet)){ $line=fgets($sheet); $line=explode("\t",$line); $query='INSERT INTO `table` VALUES ('; foreach($line as $key => &$value){ if($$key==0){ $query.="'".$value."'"; }else{ $query.=", '".$value."'"; } } $query.=');'; echo $query."\n"; mysql_query($query,$con); } fclose($file); ?> </pre> Or if you save it as CSV change the explode from "\t" to "," Hope this helps. Scott. this means I have to remove the titles(field titles) from the excel sheet. Otherwise the field titles are also inserted into the database, right? But the code have two errors: while(!feof($sheet)){ //should be $file instead of $sheet, right? $line=fgets($sheet); Thank you for helping me Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-442661 Share on other sites More sharing options...
shane07 Posted January 20, 2008 Author Share Posted January 20, 2008 I haven't tested this but it should work for a sheet saved in txt format <pre> <?php $con=mysql_connect(); mysql_select_db('test'); $sheet='test.txt'; $file=fopen($sheet,'r'); while(!feof($sheet)){ $line=fgets($sheet); $line=explode("\t",$line); $query='INSERT INTO `table` VALUES ('; foreach($line as $key => &$value){ if($$key==0){ $query.="'".$value."'"; }else{ $query.=", '".$value."'"; } } $query.=');'; echo $query."\n"; mysql_query($query,$con); } fclose($file); ?> </pre> Or if you save it as CSV change the explode from "\t" to "," Hope this helps. Scott. this means I have to remove the titles(field titles) from the excel sheet. Otherwise the field titles are also inserted into the database, right? But the code have two errors: while(!feof($sheet)){ //should be $file instead of $sheet, right? $line=fgets($sheet); Thank you for helping me The topic is solved but is there a way to exclude the field titles of the excel sheet while reading? Waiting for ur reply. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-444195 Share on other sites More sharing options...
ratcateme Posted January 20, 2008 Share Posted January 20, 2008 If all your titles are on the first or second lines you could exclude those lines from the the function change your code to this. <pre> <?php $con=mysql_connect(); mysql_select_db('test'); //number of lines to skip $skip=1; $current_line=1; $sheet='test.txt'; $file=fopen($sheet,'r'); while(!feof($file)){ $line=fgets($file); //add this if($current_line>$skip){ $line=explode("\t",$line); $query='INSERT INTO `table` VALUES ('; foreach($line as $key => &$value){ if($$key==0){ $query.="'".$value."'"; }else{ $query.=", '".$value."'"; } } $query.=');'; echo $query."\n"; mysql_query($query,$con); //add this } $current_line++; } fclose($file); ?> </pre> Sorry my server is still down so i cannot test it. but i have fixed the $sheet to $file sorry about that. Scott. Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-444242 Share on other sites More sharing options...
shane07 Posted January 20, 2008 Author Share Posted January 20, 2008 Well that was so easy. I didn't think that way. Thanx very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/86612-solved-reading-excel-sheet-and-storing-in-mysql/#findComment-444337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.