943,188 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 70248
  • PHP RSS
Apr 15th, 2008
1

code to import excel data into mysql using php

Expand Post »
Hello Sir

I want the php code to import the excel data into mysql .

pls hel me

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
padma525 is offline Offline
2 posts
since Apr 2008
Apr 15th, 2008
0

Re: code to import excel data into mysql using php

Hi,
Have you considered saving the Excel data as a CSV file, then reading the content of the file into PHP using file_get_content. You should then be able to explode the data by the comma symbol or something I am sure.

What do you think?

R.
Reputation Points: 101
Solved Threads: 74
Posting Pro in Training
blocblue is offline Offline
417 posts
since Jan 2008
Apr 16th, 2008
0

Re: code to import excel data into mysql using php

Yep. If its a csv file, then you can make use of fgetcsv to do the required operation.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Apr 16th, 2008
0

Re: code to import excel data into mysql using php

Try this... It may help you to solve your problem.

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?
  3.  
  4. /********************************************************************************************/
  5. /* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
  6. /* Edit the entries below to reflect the appropriate values
  7. /********************************************************************************************/
  8. $databasehost = "localhost";
  9. $databasename = "test";
  10. $databasetable = "sample";
  11. $databaseusername ="test";
  12. $databasepassword = "";
  13. $fieldseparator = ",";
  14. $lineseparator = "\n";
  15. $csvfile = "E:\Webcache\AA ONLINE\test1\english1.csv";
  16. /********************************************************************************************/
  17. /* Would you like to add an ampty field at the beginning of these records?
  18. /* This is useful if you have a table with the first field being an auto_increment integer
  19. /* and the csv file does not have such as empty field before the records.
  20. /* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
  21. /* This can dump data in the wrong fields if this extra field does not exist in the table
  22. /********************************************************************************************/
  23. $addauto = 0;
  24. /********************************************************************************************/
  25. /* Would you like to save the mysql queries in a file? If yes set $save to 1.
  26. /* Permission on the file should be set to 777. Either upload a sample file through ftp and
  27. /* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
  28. /********************************************************************************************/
  29. $save = 1;
  30. $outputfile = "output.sql";
  31. /********************************************************************************************/
  32.  
  33.  
  34. if(!file_exists($csvfile)) {
  35. echo "File not found. Make sure you specified the correct path.\n";
  36. exit;
  37. }
  38.  
  39. $file = fopen($csvfile,"r");
  40.  
  41. if(!$file) {
  42. echo "Error opening data file.\n";
  43. exit;
  44. }
  45.  
  46. $size = filesize($csvfile);
  47.  
  48. if(!$size) {
  49. echo "File is empty.\n";
  50. exit;
  51. }
  52.  
  53. $csvcontent = fread($file,$size);
  54.  
  55. fclose($file);
  56.  
  57. $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
  58. @mysql_select_db($databasename) or die(mysql_error());
  59.  
  60. $lines = 0;
  61. $queries = "";
  62. $linearray = array();
  63.  
  64. foreach(split($lineseparator,$csvcontent) as $line) {
  65.  
  66. $lines++;
  67.  
  68. $line = trim($line," \t");
  69.  
  70. $line = str_replace("\r","",$line);
  71.  
  72. /************************************************************************************************************
  73. This line escapes the special character. remove it if entries are already escaped in the csv file
  74. ************************************************************************************************************/
  75. $line = str_replace("'","\'",$line);
  76. /***********************************************************************************************************/
  77.  
  78. $linearray = explode($fieldseparator,$line);
  79.  
  80. $linemysql = implode("','",$linearray);
  81.  
  82. if($addauto)
  83. $query = "insert into $databasetable values('','$linemysql');";
  84. else
  85. $query = "insert into $databasetable values('$linemysql');";
  86.  
  87. $queries .= $query . "\n";
  88.  
  89. @mysql_query($query);
  90. }
  91.  
  92. @mysql_close($con);
  93.  
  94. if($save) {
  95.  
  96. if(!is_writable($outputfile)) {
  97. echo "File is not writable, check permissions.\n";
  98. }
  99.  
  100. else {
  101. $file2 = fopen($outputfile,"w");
  102.  
  103. if(!$file2) {
  104. echo "Error writing to the output file.\n";
  105. }
  106. else {
  107. fwrite($file2,$queries);
  108. fclose($file2);
  109. }
  110. }
  111.  
  112. }
  113.  
  114. echo "Found a total of $lines records in this csv file.\n";
  115.  
  116.  
  117. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sangeetha.n is offline Offline
9 posts
since Feb 2008
Apr 16th, 2008
0

sangeetha.n

Reputation Points: 10
Solved Threads: 0
Newbie Poster
sangeetha.n is offline Offline
9 posts
since Feb 2008
Oct 30th, 2008
0

Re: code to import excel data into mysql using php

How about if i want to import data in an excel file (.xls wo saving it as csv file) into mysql database table by using php.I have downloaded PHP Excel Reader from internet and create and excel template (.xls file). I also have created a simple page that allow users to browse for the .xls template and click on button 'upload and import' in order to import the data inside the .xls into mysql db. But i have no idea how to code it inside php for the import process function. i've tried some code found on G but still cannot make it work. Does anyone have any idea/reference/code on how i can make this excel import function work? Thanks.
Pja
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pja is offline Offline
12 posts
since Sep 2007
May 27th, 2009
0

Re: code to import excel data into mysql using php

Hi,

you can use http://www.dbTube.org
graphical odeling tool for database imports.

Greetings

Andreas
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FreeGroup is offline Offline
4 posts
since Jan 2007
May 29th, 2009
0

Re: code to import excel data into mysql using php

Click to Expand / Collapse  Quote originally posted by robothy ...
Hi,
Have you considered saving the Excel data as a CSV file, then reading the content of the file into PHP using file_get_content.
Since how this has already been bumped I shall add if you can export the excel file to a CSV file, phpmyadmin will allow you to import the CSV file. Very simple and nearly every server has phpmyadmin.

As a note to FreeGroup, please do not bump very old topics unless your the original author.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,003 posts
since Sep 2007
May 29th, 2009
0

Re: code to import excel data into mysql using php

I have successfully used a number of these approaches:
* Using PHPMyAdmin to import a csv file is pretty quick and simple. You will want to drop the header from the CSV file and you need to specify that the field separator is a comma (in PHPMyAdmin).

* You can use PHP to upload a CSV file and then write it to a database. This is probably most useful if you need to edit/modify any of the fields or create indexes as part of the upload process.

* You can also read an XLS file directly. I did some work on an earlier version (link to latest below). It has the advantage of being able to read each of the sheets from a workbook. One limitation of the older version however; was that it couldn't handle references from one sheet to the other. I haven't tried the latest version so I don't know if that is still the case.
http://code.google.com/p/php-excel-r...downloads/list
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
May 29th, 2009
1

Re: code to import excel data into mysql using php

You may visit : http://www.eephp.com/

ExcelExplorer is a set of PHP5 classes that can read data from a MS Excel file without using COM, ODBC, etc. It can import data from binary Excel file (XLS) using only standard PHP functions.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: please its urgent
Next Thread in PHP Forum Timeline: JOOMLA HELP Required





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC