Importing CSV data using PHP/MySQL -
I am having some problem trying to import data from CSV and there are some questions about it that I have yet to You have not succeeded in solving.
To help keep things in perspective, first here is my code (to delete it a little, count cd and db connection):
& lt; Body & gt; & Lt; Div id = "container" & gt; & Lt; Div id = "form" & gt; & Lt ;? Php $ deleterecords = "Trunket Table Tabelmem"; // Your current record mysql_query ($ deleterecords) table blank; // upload file if (isset ($ _ POST ['submit']) {if_uploaded_file ($ _ files ['filename'] ['tmp_name'])) {echo "& lt; h1 & gt;" . "File" $ _FILES ['filename'] ['name']. "Successfully uploaded." . "& Lt; / h1>"; Echo "& lt; h2 & gt; Content displayed: "; Readfile ($ _ files ['file name'] ['tmp_name']); } // database to upload import file $ handle = fopen ($ _ FILES ['filename'] ['tmp_name'], "r"); ($ $ Data = fgetcsv ($ handle, 1000, ","))! == FALSE {$ import = "INSERT to import (text, number) value ('$ data [0]', '$ data [1]')"; Mysql_query ($ import) or die (mysql_error ()); } Fclose ($ handle); Print "imported"; // View upload form} and {print "browse to file and upload & gt; \ n"; Upload new CSV by clicking on; Print "& lt; form enctype = 'multipart / form-data' action = 'upload.php' method = 'post' & gt;"; Print "file name to import:
" \ n "; Print "& lt; input size = '50' type = 'file' name = 'filename' & gt; & lt; br / & gt; \ n"; Print "& lt; input type = 'submit' name = 'submit' value = 'upload'> gt; & lt; / form & gt;"; }? & Gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Body & gt; This is basically an example optimization that I have found in many different ways after many efforts.
There are two column data of my CSV, the first text and the second integer are two columns in the table in the database, the first "text" and the second "number"
then my The questions are:
- The uploaded text is being displayed as 0 in each field and I'm not sure why
- I ended up in "" I read about reading the data that happens, if this happens, then how can I What do i do
- How can I ignore CSV's first x lines for headers etc?
- Is the data format changed in this process or is it ready for me to use in a graph? As decimal in decimal would be placed in database once?
I think everything is included, thanks in advance for any help!
Edit:
Just tested 10,000 errors and got an error:
"Fatal error: Maximum execution time of 30 seconds" / P>
Have any thoughts over?
I asked the same question on the second day Answer:
MySQL has a feature LOAD DATA INFILE , which allows it to import a CSV file directly into a single SQL query, without it We need to be processed in the loop. Simple example:
It's as simple.
There is no end, no fuss and much more faster than parsing it in PHP. MySQL manual page here:
Hopes
Comments
Post a Comment