简体   繁体   中英

Import txt file to Mysql

I have a problem and would like to know if anybody can help me..

I have a text file containing data separated by "," and I have a table where I need to import them into. Here is what I have so far.

This is my contact.php file

<?php

$dbH = mysql_connect('localhost', 'user', 'pass') or die('Could not connect to MySQL server.<br>' . mysql_error()); 


mysql_select_db('test') or die('Could not select database.<br>' . mysql_error(); 


$CSVFile = 'import.txt';


mysql_query('LOAD DATA LOCAL INFILE "import.txt" INTO TABLE wp_contact_employees FIELDS TERMINATED BY "," LINES TERMINATED BY "\r\n";') or die('Error loading data file.<br>' . mysql_error());


mysql_close($dbH);
?> 

this is my import.txt file..

kagn@test.com, Kralle G, 99998888 \\n
many@test.com, Martin N, 99876543 \\n

And in my "wp_contact_employees" I want the above to be inserted in (mail, name, tel) column.

Im new to all this php, and im stuck now :(

LOAD DATA LOCAL INFILE requires that the file pathname you specify be visible to the mysqld server process. Your server is on localhost, so that means MySQL is running on the same machine as your php. That's a good start.

Now you need to figure out the full pathname for your import.txt file. This depends on how your server is set up, but it may be something like

 /home/user3433414/public_ftp/data/import.txt

Once you have that working, you may need to change the protection of your .txt file so it is readable by the world.

It's unfortunately impossible to tell you in detail what to do without knowing the ins and outs of your hosting provider's server setup. Perhaps you should ask for help from their support team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM