By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

How to import the Excel data in mysql database using php script?

100+
P: 147
Hi,
I wrote some coding, to import excel data in mysql database using PHP Script? But my script, its not all inserting the excel data in mysql database. I don't know, what mistake did i made? Please help me out

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $strPath = realpath(basename(getenv($_SERVER['SCRIPT_FILENAME'])));
  3. echo $strPath;
  4.  
  5.  
  6. $excel_app = new COM("Excel.application") or Die ("Did not connect");
  7. $Workbook = $excel_app->Workbooks->Open('C:\san1.xls') or Die('Did not open filename');
  8. $Worksheet = $Workbook->Worksheets(1);
  9. $Worksheet->activate;
  10.  
  11.  
  12.  
  13. $username = "root";
  14. $hostname = "localhost"; 
  15.  
  16. //connection to the database
  17. $dbhandle = mysql_connect($hostname, $username) or die("Unable to connect to MySQL");
  18. echo "Connected to MySQL<br>";
  19.  
  20.  
  21. //select a database to work with
  22. $selected = mysql_select_db("academics",$dbhandle) or die("Could not select examples");
  23.  
  24. echo "hi";
  25. $i=2;  
  26. while($Worksheet->Cells->Item($i,1) != "")  
  27. {  
  28.   $strSQL = "";  
  29.   $strSQL .= "INSERT INTO tbl";  
  30.   $strSQL .= "(rollno,crseno,marks,grade,att)";  
  31.   $strSQL .= "VALUES";  
  32.   $strSQL .= "('".$Worksheet->Cells->Item($i,1)."','".$Worksheet->Cells->Item($i,2)."' ";  
  33.   #$strSQL .= ",'".$Worksheet->Cells->Item($i,3)."','".$Worksheet->Cells->Item($i,4)."' ";  
  34.   $strSQL .= ",'".$Worksheet->Cells->Item($i,3)."','".$Worksheet->Cells->Item($i,4)."') ";  
  35.   mysql_query($strSQL);  
  36.   $i++;  
  37. }  
  38.  
  39. //*** Close MySQL ***//  
  40. @mysql_close($dbhandle);  
  41.  
  42. //*** Close & Quit ***//  
  43. $excel_app->Application->Quit();  
  44. $excel_app = null;  
  45. $Workbook = null;  
  46. $Worksheet = null; 
  47.  
  48.  
  49. ?>
  50.  

Thanks in advance
May 14 '11 #1
Share this Question
Share on Google+
1 Reply


ronverdonk
Expert 2.5K+
P: 4,258
I'd suggest to use a free class for this, like
http://www.phpclasses.org/package/62...XLS-files.html
Jun 9 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.