Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load data from file

Status
Not open for further replies.

buzzt

Programmer
Joined
Oct 17, 2002
Messages
171
Location
CA
I need to be able to load the data from a CSV file from my local machine into my MySQL database on the remote server by way of a php page. I have seen some code, but nothing that really works well.

Any ideas?
 
Checkout phpmyadmin at
You can do it through their, or if you need to setup your own page, you can read the SQL they use to do it and make your own script around that.

-Rob
 
Once you have accepted the CSV file and written it to the filesystem ( there are two ways of doing it.

One is reading the file in through PHP, constructing a SQL statement from the values in the file, then executing each constructed SQL query in turn.

Another way is to use a single query containing MySQL's LOAD DATA INFILE command (
Want the best answers? Ask the best questions: TANSTAAFL!
 
Here's what I did...

I created a form to upload the data
<form action=&quot;import.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;>
<input name=&quot;file&quot; type=&quot;file&quot; size=&quot;15&quot;>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>


...and here is the import.php (the relevent part)
$result = mysql_query(&quot;LOAD DATA LOCAL INFILE '$file' INTO TABLE schedule&quot;) or die(mysql_error());

I keep getting the same error I did with other scripts:
File 'c:phpuploadtempphp24.tmp' not found (Errcode: 2)

I'm on a win2k server (if that helps).
 
What I would really like to do is something similar to what PHPMyAdmin does. Just read the data from the (local) file and insert it into the database. Is that not possible?
 
But all that will do is upload the file. How will I tell MySQL to import the data from it at the same time?
 
That would be the part of my earlier post which reads: One is reading the file in through PHP, constructing a SQL statement from the values in the file, then executing each constructed SQL query in turn.

Want the best answers? Ask the best questions: TANSTAAFL!
 
So PHP will actually break down the file and then insert the data. Is that what you mean?
 
So you will program PHP to break down the file and then insert the data, yes.

I still recommend using LOAD DATA INFILE. It sounds to me that figuring out a path or permissions problem with the temporary file would be easier than writing the code to create umpteen SQL statements.

Want the best answers? Ask the best questions: TANSTAAFL!
 
That's what I was just thinking.

Thanks.
 
Do you mean the value specified in the php.ini file?
 
The directory would most likely be found in php.ini as the value for the configuration setting upload_tmp_dir.

However, PHP, in the online manual section on handling file uploads ( relates that the superglobal predefined array $_FILES will have all the information necessary to find the file on the filesystem.

The array element $_FILES['[the form element name]'][tmp_name] contains a full path and filename.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top